The purpose of the script below is to rename multiple folders in a directory. File is named 2324[folder 1], 3242343[Folder 2), 4343[folder 3]. The purpose of script is to rename files to keep only folder 1, folder 2, and folder 3 name. I currently have 20,000 folders to rename
#!/usr/bin/python
import os
path = r"/Users/princessnaana/Desktop/Python_Examples/Naana"
directory_list = os.listdir(path)
for f in directory_list:
src = f
dst = f[f.find('[') + 1:].replace(']','')
replace_name = dst
os.rename (dst)
when I print the script the code renames the files as intended however when I run the code it does not change the names of the folder in the directory.
(Feb-16-2021, 06:32 PM)BashBedlam Wrote: [ -> ]Try it like this:
os.rename(src, dst)
I am getting the error "FileNotFoundError: [Errno 2] No such file or directory" when I use os.rename(src, dst)
Try this and tell me if it prints out the files that you want to change and the names that you want to change them to.
import os
path = r"/Users/princessnaana/Desktop/Python_Examples/Naana"
directory_list = os.listdir(path)
for f in directory_list:
if '[' in f and ']' in f :
dst = f[f.find('[') + 1:].replace(']','')
print (f, dst)
(Feb-16-2021, 07:50 PM)nyawadasi Wrote: [ -> ]I am getting the error "FileNotFoundError: [Errno 2] No such file or directory" when I use os.rename(src, dst)
If not run scripts same folder as path given then rename will not find files.
A trick is to add first
os.chdir(path)
so switch to path folder,then do rename.
An other way is to work with full path to files.
os.rename(os.path.join(path, scr), os.path.join(path, dst))
It doesn't find the src file because the os.listdir(path) only returns the names, not the paths.
I think our logic is also questionable. What if you encounter a folder that doesn't have []?
(Feb-16-2021, 08:48 PM)BashBedlam Wrote: [ -> ]Try this and tell me if it prints out the files that you want to change and the names that you want to change them to.
import os
path = r"/Users/princessnaana/Desktop/Python_Examples/Naana"
directory_list = os.listdir(path)
for f in directory_list:
if '[' in f and ']' in f :
dst = f[f.find('[') + 1:].replace(']','')
print (f, dst)
This is the results I get when I run it. But when I use the os.rename command to execute the script to change the name change on the folders I get an error with os.rename
Results below
122323-[folder 8] folder 8
754332323[folder 6] folder 6
234123-[folder] folder
98643-[folder 10] folder 10
12332-[folder 3] folder 3
.[folder7] folder7
.[folder 9] folder 9
78544-[folder 5] folder 5
.[folder 4] folder 4
3435454-[folder 2] folder 2
Thanks for your help!!
I'm sorry, I missed the most important part.
os.rename (path + f, path + dst)
import os
path = "/Users/princessnaana/Desktop/Python_Examples/Naana/"
directory_list = os.listdir(path)
for f in directory_list:
if '[' in f and ']' in f :
dst = f[f.find('[') + 1:].replace(']','')
os.rename (path + f, path + dst)

That worked!! Thank you so much. You just saved me hours upon hours !!. I appreciate it !!
You are welcome. I know what it's like to spend hours trying to figure something out
