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
  Open and read multiple text files and match words kozaizsvemira 3 3,772 Jul-07-2021, 11:27 AM
Last Post: Larz60+
  Plotting Multiple files ! Helen_145 1 227 Jun-26-2021, 03:28 PM
Last Post: snippsat
  Reading Multiple text Files in pyhton Fatim 1 253 Jun-25-2021, 01:37 PM
Last Post: deanhystad
  Running script on multiple files Afrodizzyjack 1 385 May-14-2021, 10:49 PM
Last Post: Yoriz
  Load the data from multiple source files to one table amy83 2 415 Apr-27-2021, 12:33 AM
Last Post: Pedroski55
  List of error codes to find (and count) in all files in a directory tester_V 8 890 Dec-11-2020, 07:07 PM
Last Post: tester_V
  How to merge all the files in a directory in to one file sutra 3 628 Dec-10-2020, 12:09 AM
Last Post: sutra
  Merging all file_name.log's files from directory to one and search “PerformanceINFO" sutra 0 381 Dec-09-2020, 05:14 PM
Last Post: sutra
  Copy files from subfolders into same name of subfolders at other directory rathoreanil 1 604 Oct-12-2020, 01:30 AM
Last Post: Larz60+
  Moving Files From Subdirectories To Another Directory Harshil 5 976 Oct-06-2020, 10:52 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

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