[英]Python PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: mp4 file
[英]Python - PermissionError: [WinError 32] The process cannot access the file because it is being used by another process:
我的脚本在特定目录中搜索所有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(源,dest)中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()
来关闭当前使用的文件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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.