它说它找不到当前文件,但它仍然打印出它在那里

It says it cant find the current file but it still prints it out that its there

提问人:hershey10 提问时间:10/20/2023 最后编辑:mokenhershey10 更新时间:11/15/2023 访问量:67

问:

我正在尝试运行一个程序,该程序将遍历我文件夹中的所有 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 的新手。

python excel 循环 xlsx

评论

3赞 Alderven 10/20/2023
请以文本形式分享您的代码和错误
0赞 russhoppa 10/20/2023
文档中,对于该方法,您可以指定 ExcelVersion 和 Encoding。我想知道您使用的 Excel 版本或电子表格的编码是否不适合该文件。LoadFromFile
0赞 Random Davis 10/20/2023
错误中的路径和代码中的路径完全不同;这是怎么回事?
1赞 russhoppa 10/20/2023
另外,只是一个提示,您可以尝试使用 Pandas 库来代替。通过 将 excel 文件读入 DataFrame,然后调用该 DataFrame 并通过 将其保存回 excel 文件。pandas.pydata.org/pandas-docs/stable/reference/api/....可能比您正在使用的库更容易,并且可能不会引发相同的问题。pandas.read_exceldrop_nato_excel
1赞 moken 10/20/2023
列表将只是给定目录中的文件名,即不是路径,只是像“10175.xlsx”这样的名称。工作簿的负载是,inputFile 将只是该文件名。因此,代码无法找到该文件似乎是合理的。我建议您将该行更改为,以便工作簿具有文件的完整路径。workbook.LoadFromFile(inputFile)inputFile = fileinputFile = os.path.join(path,file)

答:

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)
    # .......