rename files inside another directory in python
Question:
I’m working with python and I need to rename the files that I have inside a directory for example:
C:UserslenovoDesktopfilesfile1.txt
C:UserslenovoDesktopfilefile2.txt
C:UserslenovoDesktopfilesfile3.txt
I have these 3 files inside the files folder, and I want to change the name of these, I have my script inside another folder: C:UserslenovoDesktopapprename.py
I don’t know if this is the problem but this is what I tried and it didn’t work for me:
import os
directory = r'C:UserslenovoDesktopfiles'
count=0
for filename in os.listdir(directory):
count +=1
f = os.path.join(directory, filename)
if os.path.isfile(f):
os.rename(f, "new_file"+str(count))
UPDATE
the code simply deletes the original files and tries to create others inside the folder where I have the python script.
Answers:
You need to prepend the directory to the new files
import os
directory = r'C:UserslenovoDesktopfiles'
count=0
for filename in os.listdir(directory):
count +=1
f = os.path.join(directory, filename)
new_f = os.path.join(directory, "new_file"+str(count)+".txt")
if os.path.isfile(f):
os.rename(f, new_f)
In general, when in doubt, it’s best to use long/absolute path names when renaming/moving files. If you want to rename a file in its current directory, use the full path name in the target file name, as well. So, try changing the line:
os.rename(f, "new_file"+str(count))
to:
os.rename(f, os.path.join(directory, "new_file"+str(count)))
This absolute path will rename each file in its original directory. Otherwise, as you’ve experienced, relative file names are treated as relative to the directory of the executable.
Once you do the above, you’ll probably want to do more tweaks to get a better result, but this should get you closer to your objective.
You used a relative path for the target filename, so the operating system based the path on the current working directory. That CWD was also your script path hints that you ran the program from your script path.
You could use os.path.join
to make the path relative to your target directory. But you could also use pathlib
from pathlib import Path
directory = Path(r'C:UserslenovoDesktopfiles')
count = 0
for target in Path.iterdir():
if target.is_file():
target.replace(directory/f"newfile{count}")
count += 1
I’m working with python and I need to rename the files that I have inside a directory for example:
C:UserslenovoDesktopfilesfile1.txt
C:UserslenovoDesktopfilefile2.txt
C:UserslenovoDesktopfilesfile3.txt
I have these 3 files inside the files folder, and I want to change the name of these, I have my script inside another folder: C:UserslenovoDesktopapprename.py
I don’t know if this is the problem but this is what I tried and it didn’t work for me:
import os
directory = r'C:UserslenovoDesktopfiles'
count=0
for filename in os.listdir(directory):
count +=1
f = os.path.join(directory, filename)
if os.path.isfile(f):
os.rename(f, "new_file"+str(count))
UPDATE
the code simply deletes the original files and tries to create others inside the folder where I have the python script.
You need to prepend the directory to the new files
import os
directory = r'C:UserslenovoDesktopfiles'
count=0
for filename in os.listdir(directory):
count +=1
f = os.path.join(directory, filename)
new_f = os.path.join(directory, "new_file"+str(count)+".txt")
if os.path.isfile(f):
os.rename(f, new_f)
In general, when in doubt, it’s best to use long/absolute path names when renaming/moving files. If you want to rename a file in its current directory, use the full path name in the target file name, as well. So, try changing the line:
os.rename(f, "new_file"+str(count))
to:
os.rename(f, os.path.join(directory, "new_file"+str(count)))
This absolute path will rename each file in its original directory. Otherwise, as you’ve experienced, relative file names are treated as relative to the directory of the executable.
Once you do the above, you’ll probably want to do more tweaks to get a better result, but this should get you closer to your objective.
You used a relative path for the target filename, so the operating system based the path on the current working directory. That CWD was also your script path hints that you ran the program from your script path.
You could use os.path.join
to make the path relative to your target directory. But you could also use pathlib
from pathlib import Path
directory = Path(r'C:UserslenovoDesktopfiles')
count = 0
for target in Path.iterdir():
if target.is_file():
target.replace(directory/f"newfile{count}")
count += 1