【发布时间】:2020-04-02 22:00:05
【问题描述】:
我当前的代码是:
import os
path = os.getcwd()
filenames = os.listdir("C:/Users/Larso/Desktop/ClearEstimatesEstimate/")
filename = ('Leap Price Guide Export.xlsx')
for filename in filenames:
os.rename(os.path.join(path, filename), os.path.join(path, filename.replace(' ', '-')))
for filename in filenames:
os.rename(filename, filename.replace(" ", "-"))
但我得到错误
FileNotFoundError: [WinError 2] 系统找不到指定的文件:'C:\\Users\\larso\\Desktop\\ClearEstimatesEstimate\\AutomateExcelfileintoaspecficExcelfileformat\\AutomateExcelfileintoaspecficExcelfileformat' -> 'C:\\Users\\larso\\Desktop \\ClearEstimatesEstimate\\AutomateExcelfile 转换为specficExcel 文件格式\\AutomateExcelfile 转换为specficExcel 文件格式'任何想法如何自动化
【问题讨论】:
-
删除您的第一个 for 语句,其中您将使用您的文件名加入 CWD。span>
-
我删除了 CWD 但仍然得到相同的结果
-
您在第一个循环中重命名文件,因此在第一个循环后文件系统中不再存在带空格的文件。在第二个循环中,您仍然希望存在旧文件名(文件名中的文件名),从而导致错误。
标签: python