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
  replace or remove text from many text files Curbie 21 3,245 Jul-07-2025, 02:05 PM
Last Post: Curbie
  [SOLVED] Special characters in XML ForeverNoob 3 3,245 Dec-04-2024, 01:26 PM
Last Post: ForeverNoob
  deleting files in program files directory RRADC 6 6,649 Aug-21-2024, 06:11 PM
Last Post: snippsat
  Trying to generating multiple json files using python script dzgn989 4 4,664 May-10-2024, 03:09 PM
Last Post: deanhystad
  Filer and sort files by modification time in a directory tester_V 5 4,100 May-02-2024, 05:39 PM
Last Post: tester_V
  Loop through all files in a directory? Winfried 10 8,670 Apr-23-2024, 07:38 PM
Last Post: FortuneCoins
  Copy xml content from webpage and save to locally without special characters Nik1811 14 7,973 Mar-26-2024, 09:28 AM
Last Post: Nik1811
  uploading files from a ubuntu local directory to Minio storage container dchilambo 0 2,073 Dec-22-2023, 07:17 AM
Last Post: dchilambo
  python convert multiple files to multiple lists MCL169 6 4,618 Nov-25-2023, 05:31 AM
Last Post: Iqratech
Question Special Characters read-write Prisonfeed 1 2,139 Sep-17-2023, 08:26 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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