Have this code where I'm trying to remove all the numerical characters from strings in file names. It seems everything going all right but file names don't change.
import os
files_list = os.listdir('/mnt/c/Users/maverick/Desktop/prank/prank')
for file_name in files_list:
count = 0
l = []
for char in file_name:
if not char.isdigit():
l.append(char)
new_string = ''.join(l)
print('New string: ' + new_string)
print('Old file name: ' + file_name)
file_name = new_string
print('New file name: ' + file_name)
count = count + 1
print(files_list)
If you want to rename
you can use os.rename()
. Also, to remove all numbers from the file names you can use maketrans
on digits from string
like this.
import os
from string import digits
path = '/mnt/c/Users/maverick/Desktop/prank/prank'
files_list = os.listdir(path)
new_files_list = []
remove_digits = str.maketrans('', '', digits)
for file_name in files_list:
count = 0
new_string = file_name.translate(remove_digits)
new_files_list.append(new_string)
print('New string: ' + new_string)
os.rename(os.path.join(path, file_name), os.path.join(path, new_string))
print('Old file name: ' + file_name)
file_name = new_string
print('New file name: ' + file_name)
count = count + 1
print('Old Filenames :', files_list)
print('New Filenames :', new_files_list)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments