Python-PermissionError:[WinError 32]该进程无法访问文件,因为该文件正在被另一个进程使用:

时间:2019-07-17 16:23:04

标签: python python-3.x file-handling

我的脚本在特定目录中搜​​索所有pdf文件,然后从pdf中提取ID,并在文件内整理pdf。例如,我有:

C:\Users\user\Downloads\aa\1.pdf, with id = 3,
C:\Users\user\Downloads\aa\2.pdf, with id = 5,
C:\Users\user\Downloads\aa\3.pdf, with id = 10

我想这样组织他们:

C:\Users\user\Downloads\aa\3\1.pdf
C:\Users\user\Downloads\aa\5\2.pdf
C:\Users\user\Downloads\aa\10\3.pdf

以下脚本可以完成这项工作,但是我认为仅对于最后一个文件,它会输出以下错误:

回溯(最近通话最近):   文件“ C:\ Users \ user \ Downloads \ aa \ project.py”,第74行,在     os.rename(来源,目标) PermissionError:[WinError 32]该进程无法访问文件,因为该文件正在被另一个进程使用:'C:\ Users \ user \ Downloads \ aa \ 3.pdf'->'C:\ Users \ user \ Downloads \ aa \ 10 \ 3.pdf'

 import PyPDF2
 import re
 import glob, os
 import shutil
 import sys
 from collections import Counter
 from collections import defaultdict

 class DictList(dict):
     def __setitem__(self, key, value):
         try:
             self[key].append(value)
         except KeyError:
             super(DictList, self).__setitem__(key, value)
         except AttributeError:
             super(DictList, self).__setitem__(key, [self[key], value])

 files = glob.glob(r'C:\Users\user\Downloads\aa\*.pdf')

 gesi_id=[]
 dic = DictList()

 c = 0

 for i in files:   
     pdfFileObj = open(files[c],'rb')
     pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
     num_pages = pdfReader.numPages
     count = 0
     text = ""

 while count < num_pages:
     pageObj = pdfReader.getPage(count)
     count +=1
     text += pageObj.extractText()

 keywords = []
 keywords = re.findall(r'[0-9]\w+', text); 
 gesi_id.append(keywords[0])
 key = str(gesi_id[c])
 value = files[c]
 dic[key] = value
 c=c+1

 gesi_id_unique = []
 for x in gesi_id: 
         if x not in gesi_id_unique: 
             gesi_id_unique.append(x) 

 c=0
 if not gesi_id_unique:
   sys.exit()

 for i in gesi_id_unique:
     dirName = os.path.join('C:\\Users\\user\\Downloads\\aa\\', 
 str(gesi_id_unique[c]))
     c=c+1

     if not os.path.exists(dirName):
         os.mkdir(dirName)

 keys = list(dic)
 values = list(dic.values())
 k = 0
 v = 0
 for i in keys:
     for val in values[k]:
         source = val


          dest = os.path.join('C:\\Users\\user\\Downloads\\aa\\', 
 gesi_id_unique[k],  val.rsplit('\\', 1)[-1])
         print(gesi_id_unique[k])
         print(val.rsplit('\\', 1)[-1])
         print("Source: %s"  % source)
         print("Dest: %s" % dest)
          os.rename(source, dest)
     k = k+1

1 个答案:

答案 0 :(得分:0)

首先,我认为由于复制和过去的某些缩进被打乱了,实际上其中一部分应该是:

for i in files:   
     pdfFileObj = open(files[c],'rb')
     pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
     num_pages = pdfReader.numPages
     count = 0
     text = ""

     while count < num_pages:
          pageObj = pdfReader.getPage(count)
          count +=1
          text += pageObj.extractText()

      keywords = []
      keywords = re.findall(r'[0-9]\w+', text); 
      gesi_id.append(keywords[0])
      key = str(gesi_id[c])
      value = files[c]
      dic[key] = value
      c=c+1

要解决该问题,您只需要通过在当前位置添加pdfFileObj.close()来关闭当前使用的文件即可。

for i in files:   
     pdfFileObj = open(files[c],'rb')
     pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
     num_pages = pdfReader.numPages
     count = 0
     text = ""

     while count < num_pages:
          pageObj = pdfReader.getPage(count)
          count +=1
          text += pageObj.extractText()

      keywords = []
      keywords = re.findall(r'[0-9]\w+', text); 
      gesi_id.append(keywords[0])
      key = str(gesi_id[c])
      value = files[c]
      dic[key] = value
      c=c+1
      pdfFileObj.close()