python怎么实现多进程按序号批量修改文件名

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怎么实现多进程按序号批量修改文件名”有了一定的了解,如果想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!