Python 重命名一个文件夹中的所有文件名
2017-02-25 本文已影响51人
NieFeng1024
import os
def rename_files():
print("rename file in dir before")
file_list = os.listdir("/Users/Yan/Desktop/PythonRename")
print(file_list)
saved_path = os.getcwd()
print("current path is"+saved_path)
os.chdir("/Users/Yan/Desktop/PythonRename")
to_remove = "0123456789"
table = str.maketrans("", "", to_remove)
for file_name in file_list:
os.rename(file_name, file_name.translate(table))
file_list_new = os.listdir("/Users/Yan/Desktop/PythonRename")
print("rename file in dir end")
print(file_list_new)
rename_files()
rename.png