Python Forum
Rename Multiple files in directory to remove special characters
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rename Multiple files in directory to remove special characters
#1
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.
buran write Feb-16-2021, 06:01 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
Try it like this:

os.rename(src, dst)
Reply
#3
(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)
Reply
#4
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)
Reply
#5
(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))
Reply
#6
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 []?
Reply
#7
(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!!
Reply
#8
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)
nyawadasi likes this post
Reply
#9
Smile That worked!! Thank you so much. You just saved me hours upon hours !!. I appreciate it !!
Reply
#10
You are welcome. I know what it's like to spend hours trying to figure something out Wall
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Copy xml content from webpage and save to locally without special characters Nik1811 14 617 Mar-26-2024, 09:28 AM
Last Post: Nik1811
  uploading files from a ubuntu local directory to Minio storage container dchilambo 0 402 Dec-22-2023, 07:17 AM
Last Post: dchilambo
  python convert multiple files to multiple lists MCL169 6 1,436 Nov-25-2023, 05:31 AM
Last Post: Iqratech
Question Special Characters read-write Prisonfeed 1 582 Sep-17-2023, 08:26 PM
Last Post: Gribouillis
  Rename files in a folder named using windows explorer hitoxman 3 694 Aug-02-2023, 04:08 PM
Last Post: deanhystad
  change directory of save of python files akbarza 3 814 Jul-23-2023, 08:30 AM
Last Post: Gribouillis
  splitting file into multiple files by searching for string AlphaInc 2 816 Jul-01-2023, 10:35 PM
Last Post: Pedroski55
  Rename all files in a folder hitoxman 9 1,387 Jun-30-2023, 12:19 AM
Last Post: Pedroski55
  Using pyinstaller with .ui GUI files - No such file or directory error diver999 3 3,086 Jun-27-2023, 01:17 PM
Last Post: diver999
  Monitoring a Directory for new mkv and mp4 Files lastyle 3 1,569 May-07-2023, 12:33 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020