对文件夹下文件重新命名

下面的代码直接复制粘贴即可,

其中,下面的代码是以0开始,如果需要以001开始,把zfill(1) 改成 zfill(3)就可以。。

import os

path = r"C:\Users\Administrator\Desktop\project\3"
filelist = os.listdir(path)
count = 0
for file in filelist:
    print(file)
for file in filelist:
    Olddir = os.path.join(path, file)
    if os.path.isdir(Olddir):
        continue
    filename = os.path.splitext(file)[0]
    filetype = os.path.splitext(file)[1]
    Newdir = os.path.join(path, str(count).zfill(1) + filetype)
    os.rename(Olddir, Newdir)

    count += 1

猜你喜欢

转载自blog.csdn.net/qq_44666320/article/details/126703202