Python-PermissionError:[WinError 32]进程无法访问该文件,因为其他进程正在使用该文件:
“我的脚本”搜索特定目录中的所有pdf文件,然后从pdf中提取id并在文件中组织pdf。例如,我有:Python-PermissionError:[WinError 32]进程无法访问该文件,因为其他进程正在使用该文件:,python,python-3.x,file-handling,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\use
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行,在
重命名操作系统(源、目标)
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
导入PyPDF2
进口稀土
导入全局操作系统
进口舒蒂尔
导入系统
从收款进口柜台
从集合导入defaultdict
课堂听写列表(dict):
定义设置项(自身、键、值):
尝试:
self[key]。追加(值)
除KeyError外:
super(DictList,self)。\uuuuu setitem\uuuuuuuu(键,值)
除属性错误外:
super(DictList,self)。\uuuuuu setitem\uuuuuuu(键,[self[key],值])
files=glob.glob(r'C:\Users\user\Downloads\aa\*.pdf')
gesi_id=[]
dic=DictList()
c=0
对于文件中的i:
pdfFileObj=open(文件[c],'rb')
pdfReader=PyPDF2.PdfileReader(PdfileObj)
num_pages=pdfReader.numPages
计数=0
text=“”
当计数
首先,我认为由于复制和过去的一些缩进受到干扰,事实上有一部分应该是:
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
我看到一个调用
open
,但是您在哪里关闭文件?可能它被其他程序(如acrobat PDF reader)使用。当一个CSV文件同时在excel中打开时,我也遇到了同样的问题。它似乎可以工作,但当我有另一个文件并以名称5.pdf和id=3第二次运行脚本时,我出现了以下错误:FileNotFoundError:[WinError 2]系统找不到指定的文件:“C'->”C:\\Users\\user\\Downloads\\aa\\3\\C似乎仅从Source的val值中获取“C”,因为此PDF不包含书面文本。相反,它包含包含文本的图像。
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()