重命名文件夹中的文件

时间:2019-04-16 12:41:35

标签: python-3.7

import os
def rename_files():
    #(1) get file names from a folder
    file_list = os.listdir(r"C:\Users\Premkumar\Documents\Prem Collections\testcode\prank")
    #print(file_list)
    saved_path = os.getcwd()
    print("Current working Directory is "+saved_path)
    os.chdir(r"C:\Users\Premkumar\Documents\Prem Collections\testcode\prank")
    #(2) for each file, rename filename
    for file_name in file_list:
        os.rename(file_name, file_name.translate(None, "0123456789"))
    os.chdir(saved_path)

rename_files()

0 个答案:

没有答案