网络编程
位置:首页>> 网络编程>> Python编程>> python实现多进程按序号批量修改文件名的方法示例

python实现多进程按序号批量修改文件名的方法示例

作者:团长sama  发布时间:2021-10-30 08:33:42 

标签:python,多进程,文件名

本文实例讲述了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()

注意事项

  1. 多进程下python vscode终端debug不报错 注意可能潜在的bug

  2. os.rename()无法将文件命名成已经存在的文件,否则会报错

希望本文所述对大家Python程序设计有所帮助。

来源:https://blog.csdn.net/sinat_24899403/article/details/87358709

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com