Replacing Filename characters with python

前端 未结 4 1126
借酒劲吻你
借酒劲吻你 2021-02-06 02:43

I have some code which adds the word \"_manual\" onto the end of a load of filenames.. I need to change the script so that it deletes the last two letters of the filename (ES)

4条回答
  •  半阙折子戏
    2021-02-06 03:09

    you could do:

    for filename in filenames:
        print(filename) #should display AC-5400ES.txt
        filename = filename.replace("ES.txt","ES_manual.txt")
        print(filename) #should display AC-5400ES_manual.txt
        fullpath = os.path.join(root, filename)
        os.rename(fullpath, filename)
    

提交回复
热议问题