时间:2020-12-24 python教程 查看: 900
本文实例讲述了python实现多进程按序号批量修改文件名的方法。分享给大家供大家参考,具体如下:
文件名命名方式如图,是数字序号开头,但是中间有些文件删掉了,序号不连续,这里将序号连续起来,总的文件量有40w+,故使用多进程
import os
import re
from multiprocessing import Pool
def getAllFilePath(pathFolder,filter=[".jpg",".txt"]):
#遍历文件夹下所有图片
result=[]
#maindir是当前搜索的目录 subdir是当前目录下的文件夹名 file是目录下文件名
for maindir,subdir,file_name_list in os.walk(pathFolder):
for filename in file_name_list:
apath=os.path.join(maindir,filename)
ext=os.path.splitext(apath)[1]#返回扩展名
if ext in filter:
result.append(apath)
return result
def changName(filePath,changeNum):
fileName=os.path.basename(filePath)
dirName=os.path.dirname(filePath)
pattern = re.compile(r'\d+')
if len(pattern.findall(filePath))!=0:
numInFileName=str(int(pattern.findall(fileName)[0])-changeNum)
newFileName=pattern.sub(numInFileName,fileName)
os.rename(filePath,os.path.join(dirName,newFileName))
print('{1} is changed as {0}'.format(newFileName,fileName))
def changeNameByList(fileList,changNum):
print('fileList len is:{}'.format(len(fileList)))
for fileName in fileList:
changName(fileName,changNum)
print(fileName,' is done!')
if __name__ =='__main__':
allFilePath=getAllFilePath(r'E:\Numberdata\4')
n_total=len(allFilePath)
n_process=8 #8线程
#每段子列表长度
length=float(n_total)/float(n_process)
indices=[int(round(i*length)) for i in range(n_process+1)]
sublists=[allFilePath[indices[i]:indices[i+1]] for i in range(n_process)]
#生成进程池
p=Pool(n_process)
for i in sublists:
print("sublist len is {}".format(len(i)))
p.apply_async(changeNameByList, args=(i,161130))
p.close()
p.join()
希望本文所述对大家Python程序设计有所帮助。