使用python批量修改文件夹里面文件的名称。修改为当前时间的时候,由于毫秒级会同时改名多个文件,会出现重名现象,因此为每一个修改文件添加一个三位数的随机数。
r=random.randint(0,1001)
设置时间为毫秒级
t = time.time()
t=int(round(t*1000))
在本代码中将改名前与改名后的文件名输出作为直观的比较。
完整代码见下:
#!/user/bin/env python
# _*_ utf-8 _*_
#批量修改文件名
#批量修改图片文件名
import os
import re
import sys
import random
import time
import json
def renameall():
fileList=os.listdir(r"D:\picture\mixture\new\middle\41-60")
#print("修改前:"+str(fileList))
currentpath= os.getcwd()
os.chdir(r"D:\picture\mixture\new\middle\41-60")
for fileName in fileList:
Name=os.path.splitext(fileName)[0] #获取文件名,去除后缀名
Name = Name + str(".")
print("修改前"+str(Name))
t