Replacing Filename characters with python

前端 未结 4 1070
借酒劲吻你
借酒劲吻你 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:16

    Try this:

    import os
    pathiter = (os.path.join(root, filename)
        for root, _, filenames in os.walk(folder)
        for filename in filenames
    )
    for path in pathiter:
        newname =  path.replace('ES.txt', '_ES_manual.txt')
        if newname != path:
            os.rename(path,newname)
    

提交回复
热议问题