Batch Renaming of Files in a Directory

前端 未结 13 1501
孤街浪徒
孤街浪徒 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:07

    as to me in my directory I have multiple subdir, each subdir has lots of images I want to change all the subdir images to 1.jpg ~ n.jpg

    def batch_rename():
        base_dir = 'F:/ad_samples/test_samples/'
        sub_dir_list = glob.glob(base_dir + '*')
        # print sub_dir_list # like that ['F:/dir1', 'F:/dir2']
        for dir_item in sub_dir_list:
            files = glob.glob(dir_item + '/*.jpg')
            i = 0
            for f in files:
                os.rename(f, os.path.join(dir_item, str(i) + '.jpg'))
                i += 1
    

    (mys own answer)https://stackoverflow.com/a/45734381/6329006

提交回复
热议问题