Python实现遍历文件夹下所有文件并重命名排序

存在问题:
1.多次重复运行会导致文件丢失
2.文件原有顺序会被打乱

import os
count = 1
path = ""
filelist = os.listdir(path)

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(4) + filetype)
    os.rename(Olddir, Newdir)

    count += 1

    原文作者:杰尼龟1231
    原文地址: https://blog.csdn.net/hxc0520/article/details/121400747
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞