Batch Renaming of Files in a Directory

前端 未结 13 1498
孤街浪徒
孤街浪徒 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 09:51

    I have this to simply rename all files in subfolders of folder

    import os
    
    def replace(fpath, old_str, new_str):
        for path, subdirs, files in os.walk(fpath):
            for name in files:
                if(old_str.lower() in name.lower()):
                    os.rename(os.path.join(path,name), os.path.join(path,
                                                name.lower().replace(old_str,new_str)))
    

    I am replacing all occurences of old_str with any case by new_str.

提交回复
热议问题