Python Forum
pyPDF2 PDFMerger close pensding file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pyPDF2 PDFMerger close pensding file
#1
Hello to everyone!
I have a problem using pyPDF2: I merge some pdf in a directory and I want to delete it after the job is done, but when I try to do it, I can't because one of them (the single pdf) was open by the pdfmerger and I can't delete it. The error is:

Error:
Error in line 395: [WinError 32] Impossibile accedere al file. Il file รจ utilizzato da un altro processo: 'S:\\QUAREP\\Controlli\\12000609\\00_Report\\Vecchio_Nuovo\\temp\\12000609_220726_IP_Figura_2_su_12000609_220616_IP_Figura_2.pdf' Traceback (most recent call last): File "Vecchio_Nuovo_Da_CAD_Pdf_Merge_Snellito.py", line 396, in <module> File "shutil.py", line 513, in rmtree return _rmtree_unsafe(path, onerror) File "shutil.py", line 397, in _rmtree_unsafe onerror(os.unlink, fullname, sys.exc_info()) File "shutil.py", line 395, in _rmtree_unsafe os.unlink(fullname)
The code I used is:

rootDir=temp_path

for dirName,subDir, fileList in os.walk(rootDir, topdown=False):
	merger = PdfFileMerger()
	for fname in fileList:
		merger.append(PdfFileReader(open(os.path.join(dirName, fname),'rb')))
		print (fname)
merger.write(str(path2)+(nome_confronto)+".pdf")

folder=temp_path
for filename in os.listdir(folder):
	file_path = os.path.join(folder, filename)
	try:
		if os.path.isfile(file_path) or os.path.islink(file_path):
			os.unlink(file_path)
		elif os.path.isdir(file_path):
			shutil.rmtree(file_path)
	except Exception as e:
		print('Failed to delete %s. Reason: %s' % (file_path, e))
How can I resolve it?
Larz60+ write Jul-27-2022, 07:44 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Fixed for you this time. Please use bbcode tags on future posts.
Reply
#2
pdfmerger is not leaving the file open, you are. Here you open a file and never close it.
merger.append(PdfFileReader(open(os.path.join(dirName, fname),'rb')))
I would use a context manager to automatically close the file.
with open(os.path.join(dirName, fname),'rb') as file:
    merger.append(PdfFileReader(file))
Reply
#3
(Jul-27-2022, 03:38 PM)deanhystad Wrote: pdfmerger is not leaving the file open, you are. Here you open a file and never close it.
merger.append(PdfFileReader(open(os.path.join(dirName, fname),'rb')))
I would use a context manager to automatically close the file.
with open(os.path.join(dirName, fname),'rb') as file:
    merger.append(PdfFileReader(file))

Thank you very much!! It works perfectly!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PyPDF2 deprecation problem gowb0w 5 3,900 Sep-21-2023, 12:38 PM
Last Post: Pedroski55
  ModuleNotFoundError: No module named 'PyPDF2' Benitta2525 1 1,468 Aug-07-2023, 05:32 AM
Last Post: DPaul
  Save and Close Excel File avd88 0 2,973 Feb-20-2023, 07:19 PM
Last Post: avd88
  Pypdf2 will not find text standenman 2 920 Feb-03-2023, 10:52 PM
Last Post: standenman
  PyPDF2 processing problem Pavel_47 6 9,732 May-04-2021, 06:58 AM
Last Post: chaitanya
  How to close file handler or log file on each iteration Mekala 3 5,075 Aug-16-2020, 03:15 PM
Last Post: snippsat
  How To Find an Opening and Closing String, Copying Open/Close/Contents to New File davidshq 1 2,019 Mar-03-2020, 04:47 AM
Last Post: davidshq
  How can I Open and close .py file from python scripts SayHiii 9 5,708 Dec-17-2019, 06:10 AM
Last Post: Malt
  Problem with installing PyPDF2 Pavel_47 2 6,003 Nov-10-2019, 02:58 PM
Last Post: Pavel_47
  Does python guarantees that file.write() works without file.close() ? kryptomatrix 7 3,903 Oct-21-2019, 11:28 AM
Last Post: kryptomatrix

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020