由于OPEND PDF文件而失败的删除目录
大家好,我是新来的,
程序说明:我目前正在开发一个python程序,该程序取消了包含PDF的文件夹。然后,它将PDF的页面分成单页PDF,并将它们分为另一个路径上的文件夹(“/georeferenzieren/0;/georeferenzieren/1; ...),具体取决于页码。 对于代码的这个特定部分,我遵循 tutorial 。
问题:都可以很好地工作,但是当我尝试删除临时文件夹时,显示错误的文件夹的第一个文件仍在另一个过程中使用。
(ger)
执行期:[WinError 32] Der Prozess Kann Nicht Auf Die Datei Zugreifen,Da Sie von Einem Anderen Anderen prozess prozess verwendet wird:工作\ testumgebung \ temp \ 20220524_0109_v01_auskunft_01_a3_h.pdf'
(en)
执行手术:[WinError 32]该过程无法访问该文件,因为另一个过程正在使用:'e:\ intern \ programMieren \ python工作\ TSTEMUMGEBUNG \ temp \ temp \ 20220524_0109_V01_AUSKUNFT01_AUSKUNFT_AUSKUNFT_AUSKUNFT_AUSKUNFT_01_A3_A3_A3_A3_A3_PDFFF'
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
执行 强>
- 重新启动PC以确保没有程序使用PDF。我认为仍然发生错误,因为PDF是由Python文件操作的。
- 添加
pdf.close()
(请参见下面的代码) - 添加
pdf.merger.close()
,例如这个家伙建议,因为我不使用合并,所以它不起作用。 (请参见下面的代码)
代码:
#variables from the code befor this passage
run = 0
tempPath = "E:\Intern\Programmieren\Python for Work\Testumgebung\temp"
geoPath = "E:\Intern\Programmieren\Python for Work\Testumgebung\Georeferenzieren"
#splitting up PDFs to singel Page PDFs
#https://www.thepythoncode.com/article/split-pdf-files-in-python
print("###Split up PDFs")
file2pages = {
0: [0,1], 1: [1,2], 2: [2,3], 3: [3,4], 4: [4,5],
}
for root, directories, file in os.walk(tempPath):
for file in file:
run = run + 1
filePath = os.path.join(tempPath, file)
pdf = Pdf.open(filePath)
newPdfFiles = [Pdf.new() for i in file2pages]
newPdfIndex = 0
for n, page in enumerate(pdf.pages):
if n in list(range(*file2pages[newPdfIndex])):
newPdfFiles[newPdfIndex].pages.append(page)
else:
# make a unique filename based on original file name plus the index
pdfPath = os.path.join(geoPath, str(newPdfIndex), file)
outputFilename = f"{pdfPath}-{newPdfIndex}.pdf"
# save the PDF file
newPdfFiles[newPdfIndex].save(outputFilename)
newPdfIndex += 1
# add the `n` page to the `newPdfIndex` file
newPdfFiles[newPdfIndex].pages.append(page)
#save last PDF file
pdfPath = os.path.join(geoPath, str(newPdfIndex), file)
outputFilename = f"{pdfPath}-{newPdfIndex}.pdf"
newPdfFiles[newPdfIndex].save(os.path.join(geoPath, "1. Page", outputFilename))
print(f"Splitting up the {run}. PDF.")
#pdf.close()
#Pdf.merger.close()
#deleting temp folder
print(end = "\n")
print("#Removing temp folder...")
shutil.rmtree(tempPath)
time.sleep(2)
问题:我想知道是否有办法在删除文件夹之前关闭已使用的PDF
Hey guys I'm new here,
Program explanation: I'm currently working on a python program that unzips folders which include PDFs to an "temp" folder. It then splits the pages of the PDFs to single page PDFs and sorts them into folders on another path ("/Georeferenzieren/0; /Georeferenzieren/1; ...) depending on the page number.
For this specific part of the code I followed this guys tutorial.
Problem: That all works perfectly fine, but when I try to delete the temp folder, an error is displayed that the first file of the folder is still being used by another process.
(ger)
PermissionError: [WinError 32] Der Prozess kann nicht auf die Datei zugreifen, da sie von einem anderen Prozess verwendet wird: 'e:\Intern\Programmieren\Python for Work\Testumgebung\temp\20220524_0109_V01_Auskunft_01_A3_H.pdf'
(en)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'e:\Intern\Programmieren\Python for Work\Testumgebung\temp\20220524_0109_V01_Auskunft_01_A3_H.pdf'
What I tried:
- Restarting PC to make sure no program uses the PDF. I think the error still occured because the PDF is opend by the python file.
- adding
pdf.close()
(pls see code below) - adding
Pdf.merger.close()
, like this guy suggested, which didn't work since I'm not using the merger. (pls see code below)
Code:
#variables from the code befor this passage
run = 0
tempPath = "E:\Intern\Programmieren\Python for Work\Testumgebung\temp"
geoPath = "E:\Intern\Programmieren\Python for Work\Testumgebung\Georeferenzieren"
#splitting up PDFs to singel Page PDFs
#https://www.thepythoncode.com/article/split-pdf-files-in-python
print("###Split up PDFs")
file2pages = {
0: [0,1], 1: [1,2], 2: [2,3], 3: [3,4], 4: [4,5],
}
for root, directories, file in os.walk(tempPath):
for file in file:
run = run + 1
filePath = os.path.join(tempPath, file)
pdf = Pdf.open(filePath)
newPdfFiles = [Pdf.new() for i in file2pages]
newPdfIndex = 0
for n, page in enumerate(pdf.pages):
if n in list(range(*file2pages[newPdfIndex])):
newPdfFiles[newPdfIndex].pages.append(page)
else:
# make a unique filename based on original file name plus the index
pdfPath = os.path.join(geoPath, str(newPdfIndex), file)
outputFilename = f"{pdfPath}-{newPdfIndex}.pdf"
# save the PDF file
newPdfFiles[newPdfIndex].save(outputFilename)
newPdfIndex += 1
# add the `n` page to the `newPdfIndex` file
newPdfFiles[newPdfIndex].pages.append(page)
#save last PDF file
pdfPath = os.path.join(geoPath, str(newPdfIndex), file)
outputFilename = f"{pdfPath}-{newPdfIndex}.pdf"
newPdfFiles[newPdfIndex].save(os.path.join(geoPath, "1. Page", outputFilename))
print(f"Splitting up the {run}. PDF.")
#pdf.close()
#Pdf.merger.close()
#deleting temp folder
print(end = "\n")
print("#Removing temp folder...")
shutil.rmtree(tempPath)
time.sleep(2)
Question: I would like to know if there is a way to close the used PDF before deleting the folder
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个干净的代码,可以做同样的代码。在我的机器上测试。
您的代码似乎存在许多问题。因此,重新实现了它。使用观察
的使用情况
,如果您仍然遇到错误,则可能需要检查计算机上的文件权限。
编辑:基于将PDF的每个页面转换为单独的PDF的要求,更新了代码以与具有可变的页面数量的PDF一起使用。
Here's a clean code that does the same. Tested on my machine.
Your code seems to have numerous issues. Hence re-implemented it. Observe the usage of
with
if you still get error, you might want to check the file permissions on your machine.
EDIT: Based on the requirement to convert each page of the pdf into a separate pdf, updated the code to work with pdfs having variable number of pages.