Batch Renaming of Files in a Directory

前端 未结 13 1358
孤街浪徒
孤街浪徒 2020-11-27 09:41

Is there an easy way to rename a group of files already contained in a directory, using Python?

Example: I have a directory full of *.doc files an

相关标签:
13条回答
  • 2020-11-27 10:14

    This code will work

    The function exactly takes two arguments f_patth as your path to rename file and new_name as your new name to the file.

    import glob2
    import os
    
    
    def rename(f_path, new_name):
        filelist = glob2.glob(f_path + "*.ma")
        count = 0
        for file in filelist:
            print("File Count : ", count)
            filename = os.path.split(file)
            print(filename)
            new_filename = f_path + new_name + str(count + 1) + ".ma"
            os.rename(f_path+filename[1], new_filename)
            print(new_filename)
            count = count + 1
    
    0 讨论(0)
提交回复
热议问题