Replacing Filename characters with python

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

    for root, dirs, filenames in os.walk(folder):
        to_write = ['root == %s\n' % root]
    
        for filename in filenames:
            filename_zero, fileext = os.path.splitext(filename)
            newname = "%s_%s_manual%s" % (filename_zero[:-2],filename_zero[-2:],fileext)
    
            tu = (os.path.join(root, filename), os.path.join(root, newname))
    
            to_write.append('%s --> %s\n' % tu)
            os.rename(*tu)
    
        print '\n'.join(to_write)
    

提交回复
热议问题