Issue With Renaming A Bunch Of Files In A Directory Using Python
import os def rename_files(): file_list = os.listdir(r'G:\Python_Learning\prank') print(file_list) saved_path =os.getcwd() print('Current work
Solution 1:
Use str.strip().
import os
defrename_files():
file_list = os.listdir(r"G:\Python_Learning\prank")
print(file_list)
saved_path =os.getcwd()
print("Current working directory is "+saved_path)
os.chdir(r"G:\Python_Learning\prank")
for file_name in file_list:
os.rename(file_name,file_name.strip("0123456789"))
os.chdir(saved_path)
rename_files()
Solution 2:
os.rename(file_name, file_name.strip("01234567789"))
Post a Comment for "Issue With Renaming A Bunch Of Files In A Directory Using Python"