Feels like I have tried almost everything - and can't for the life of me work out what's going wrong in this code. I've run it on a much simpler example (just os.rename, no manipulations, file_name provided without use of variables etc.) and it worked as expected.
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'
Any guidance at all would be amazing - really can't move on from this problem!
Thanks so much
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))
have also tried
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)
os.rename
is working. your code does not work becauseC:\\Users\\Tom\\Downloads\\oldnameoffile.xlsx
does not exist – DeepSpace Dec 6 '18 at 13:31os.listdir()
. – glglgl Dec 6 '18 at 13:33print(f)
so we see what it displays. – Sembei Norimaki Dec 6 '18 at 13:37