Python-权限错误: [WinError 32] The process cannot access the file because it is being used by another process:

标签 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

最佳答案

首先,我认为由于复制和过去,一些缩进受到干扰,实际上有一部分应该是:

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()

关于Python-权限错误: [WinError 32] The process cannot access the file because it is being used by another process:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57080412/

相关文章:

python - 了解移动到不同机器时的虚拟环境行为

python - 在 Amazon Elastic Beanstalk 上安装 Anaconda

python - 确定矩阵中具有重复行的行数

python - 如何解决 "TypeError: unorderable types: str() < int()"错误?

python - Docker:执行Python脚本

c++ - 错误地读取文件

python - Postgres 数据库安全 : what to store in environment variables?

python - 如何在没有 SymPy 的情况下计算给定函数的积分?

c++ - 以下代码是单个文件中的文件读/写。但是此代码无法创建文件

c - c - 如何在文件中写入两个字符串,用空格或逗号分隔?