Rename multiple files in Python

后端 未结 6 1124
梦如初夏
梦如初夏 2020-12-01 08:14

How can I rename the following files:

abc_2000.jpg
abc_2001.jpg
abc_2004.jpg
abc_2007.jpg

into the following ones:

year_200         


        
6条回答
  •  日久生厌
    2020-12-01 09:20

    import os
    import glob
    
    path = 'C:\\Users\\yannk\\Desktop\\HI'
    file_num = 0
    for filename in glob.glob(os.path.join(path, '*.jpg')):
        os.rename(filename, path + '\\' + str(file_num) + '.jpg')
        file_num += 1
    

    This is to rename all files in a folder to the indentation of their position in the folder you should be able to scrap this code up and use it for your purpose. THIS is for people who are on windows and therefore os.cwd or os.getcwd do not work

提交回复
热议问题