python 它说它找不到当前文件,但它仍然打印出来,它的存在

bmp9r5qi  于 2023-11-15  发布在  Python
关注(0)|答案(1)|浏览(97)

我试图运行一个程序,将通过我的文件夹中的所有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()

字符串
下面是error

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新手。

jq6vz3qz

jq6vz3qz1#

列表中的项目是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)
    # .......

字符串

相关问题