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:
How can I resolve it?
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:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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)) |
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.
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.