Python's os library-batch modify file names

Use python's os library to easily modify file names in batches, just modify the path and the corresponding public prefix and suffix by yourself!

import os


if __name__=='__main__':
    path = './test_img/line'
    # 获取该目录下所有文件,存入列表中
    fileList = os.listdir(path)

    n = 0
    for i in fileList:
        print(i)
        # 设置旧文件名(就是路径+文件名)
        oldname = path + os.sep + fileList[n]  # os.sep添加系统分隔符

        # 设置新文件名 (路径+分隔符+公共前缀+序号+后缀)
        newname = path + os.sep + 'line' + str(n + 1) + '.jpg'

        os.rename(oldname, newname)  # 用os模块中的rename方法对文件改名
        print(oldname, '======>', newname)

        n += 1

Effect picture:

Guess you like

Origin blog.csdn.net/weixin_44593822/article/details/114878921