提问人:hershey10 提问时间:10/20/2023 最后编辑:mokenhershey10 更新时间:11/15/2023 访问量:67
它说它找不到当前文件,但它仍然打印出它在那里
It says it cant find the current file but it still prints it out that its there
问:
我正在尝试运行一个程序,该程序将遍历我文件夹中的所有 excel 文件并删除空行。
当我尝试运行它时,我收到一个错误,说我在循环中的当前文件找不到。
我尝试在循环中打印出当前文件,它打印出来它在那里。
为什么它不起作用?
正如您在错误屏幕截图中看到的那样,它打印出当前文件的名称,但后来它说找不到它。
请帮忙
import os
from spire.xls import *
from spire.common import *
#directory that needs to get fixed:
path = 'C:/Users/paulina.rios/Desktop/Teacher Sheets - Copy (2) - Copy/'
#creates a list with directory file names
list = os.listdir(path)
# Goes through each file in the directory
for file in list:
print(file)
# Specify the input and output file paths
inputFile = file
outputFile = "Edited.xlsx"
# Create a workbook instance
workbook = Workbook()
# Load an Excel file
workbook.LoadFromFile(inputFile)
# Get the first worksheet
sheet = workbook.Worksheets[0]
# Delete blank rows from the worksheet
for i in range(sheet.Rows.Length - 1, -1, -1):
if sheet.Rows[i].IsBlank:
sheet.DeleteRow(i + 1)
# Save the result file
workbook.SaveToFile(outputFile, ExcelVersion.Version2013)
workbook.Dispose()
这是错误:
C:\Users\paulina.rios\PycharmProjects\python Project\venv\Scripts\python.exe "C:\Users\paulina.rios\AppData\Roaming\JetBrains\PyCharmCE2023.1\scratches\this one.py"
10175.xlsx
Traceback (most recent call last):
File "C:\Users\paulina.rios\AppData\Roaming\JetBrains\PyCharmCE2023.1\scratches\this one.py", line 24, in <module>
workbook.LoadFromFile(inputFile)
File "C:\Users\paulina.rios\PycharmProjects\python Project\venv\lib\site-packages\plum\function.py", line 642, in __call__
return self.f(self.instance, *args, **kw_args)
File "C:\Users\paulina.rios\PycharmProjects\python Project\venv\lib\site-packages\plum\function.py", line 592, in __call__
return _convert (method (*args, **kw_args), return_type)
File "C:\Users\paulina.rios\PycharmProjects\pythonProject\venv\lib\site-packages\spire\xls\Workbook.py", line 602, in LoadFromFile
raise ArgumentError(intPtr.typeName)
ctypes. ArgumentError: IO_FileNotFound_FileName, C:\Users\paulina.rios\AppData\Roaming\JetBrains\PyCharmCE2023.1\scratches\10175.xlsx
Process finished with exit code 1
我是 Python 的新手。
答:
0赞
Dheeraj Malik
11/15/2023
#1
列表中的项目是 Excel 文件的名称,而不是其完整路径。您应该像这样更改代码:
#directory that needs to get fixed:
path = 'C:/Users/paulina.rios/Desktop/Teacher Sheets - Copy (2) - Copy/'
#creates a list with directory file names
list = os.listdir(path)
# Goes through each file in the directory
for file in list:
file_path = os.path.join(path, file)
# Create a workbook instance
workbook = Workbook()
# Load an Excel file
workbook.LoadFromFile(file_path)
# .......
评论
LoadFromFile
pandas.read_excel
drop_na
to_excel
workbook.LoadFromFile(inputFile)
inputFile = file
inputFile = os.path.join(path,file)