File Renaming In Python
October 29, 2022
Renaming:
# http://www.pillalamarri.in/python/file-renaming-in-python/
import os
for filename in os.listdir("/Users/User/Desktop/uni/change file names"):
if filename.startswith("cheese_"):
os.rename(filename, filename[7:])
Removing:
file_name = "hello23you.jpg"
print(file_name)
result = "".join([i for i in file_name if not i.isdigit()])
print(result)
# http://www.pillalamarri.in/python/file-renaming-in-python/
Project:
import os
for filename in os.listdir("/Users/User/Desktop/uni/change file names/workedOn"):
path = os.getcwd()
os.chdir("/Users/User/Desktop/uni/change file names/workedOn")
result = "".join([i for i in filename if not i.isdigit()])
os.rename(filename, result)
os.chdir(path)
# http://www.pillalamarri.in/python/file-renaming-in-python/
Listing:
import os
x = os.listdir("/Users/User/Desktop/uni/change file names/workedOn")
print(x)
# http://www.pillalamarri.in/python/file-renaming-in-python/
Posted in Python