感觉就像我已经尝试了几乎所有东西一样,并且无法终生确定这段代码出了什么问题。我已经在一个简单得多的示例上运行了它(只是os.rename,没有任何操作,提供了不使用变量的file_name等),并且按预期工作。
Traceback (most recent call last):
File "C:/Users/Tom/PycharmProjects/Projects/rename files.py", line 35, in <module>
os.rename(oldfilename, newfilename)
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\\Users\\Tom\\Downloads\\oldnameoffile.xlsx' -> 'C:\\Users\\Tom\\Downloads\\newnameoffile.xlsx'
任何指导都将是惊人的-真的无法摆脱这个问题!
非常感谢
import os
os.chdir(r"C:\Users\Tom\Downloads")
s_dir = r"C:\Users\Tom\Downloads"
for f in os.listdir():
[manipulation of filename]
...
...
final_file_name_1 = "1_{}.xlsx".format(file_brand)
final_file_name_2 = "2_{}.xlsx".format(file_brand)
if file_name[0:1] == "1":
os.rename(os.path.join(s_dir, f), os.path.join(s_dir, final_file_name_1))
elif file_name[0:1] == "2":
os.rename(os.path.join(s_dir, f), os.path.join(s_dir, final_file_name_2))
也尝试过
final_file_name_1 = "1_{}.xlsx".format(file_brand)
final_file_name_2 = "2_{}.xlsx".format(file_brand)
oldfilename = str(s_dir + "\\" + f)
if file_name[0:1] == "1":
newfilename = str(s_dir + "\\" + final_file_name_1)
os.rename(oldfilename, newfilename)
elif file_name[0:1] == "2":
newfilename = str(s_dir + "\\" + final_file_name_2)
os.rename(oldfilename, newfilename)