Batch Renaming of Files in a Directory

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

    Be in the directory where you need to perform the renaming.

    import os
    # get the file name list to nameList
    nameList = os.listdir() 
    #loop through the name and rename
    for fileName in nameList:
        rename=fileName[15:28]
        os.rename(fileName,rename)
    #example:
    #input fileName bulk like :20180707131932_IMG_4304.JPG
    #output renamed bulk like :IMG_4304.JPG
    

提交回复
热议问题