使用python 批量修改文件夹名字或者文件名字
import os
import natsort
path = 'path'
fileList = natsort.natsorted(os.listdir(path))
os.chdir(path)
num = 1
for fileName in fileList:
os.rename(fileName, ('测试-' + str(num)))
num = num + 1
使用python 批量修改文件夹下面的文件 两层循环(这里修改的是图片)
import os
import natsort
path = '3'
fileList = natsort.natsorted(os.listdir(path))
print("修改前:" + str(fileList)[1])
currentpath = os.getcwd()
os.chdir(path)
num = 1
for fileName in fileList:
picList = os.listdir(fileName)
fileNum = 1
for picName in picList:
pattern = picName.split('.')
currentpath = os.getcwd()
os.chdir(fileName)
os.rename(picName, (str(fileNum) + '.' + pattern[1]))
os.chdir(currentpath)
fileNum = fileNum + 1