关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻公共列表

python批量重命名文件为000开头

发布时间:2022/1/10 9:26:12
香港云服务器

有时候遇到文件夹中很多文件,需要统一重命名为0001-9999样式,可以使用如下python代码,filepath根据自己的实际情况进行修改

 import os
filepath = "C:\\Users\\Administrator\\Videos\\Captures\\"
filelist = os.listdir(filepath)
n = 1
for file in filelist:
m = '%04d' % n
oldname = filepath + file
newname = filepath + str(m) + '.png'
os.rename(oldname,newname)
print(oldname,'----------',newname)
n+=1