1

I got always the message: PermissionError: [WinError 32] The process cannot access the file because it is being used by another process Yes, i read [enter link description here][1] but it didn't help me.

# pdf_merger2.py
import glob,os,shutil
os.getcwd()
from PyPDF2 import PdfFileMerger
os.chdir(r"auf_comp")
def merger(output_path, input_paths):
    pdf_merger = PdfFileMerger()
    file_handles = []
    
    for path in input_paths:
        pdf_merger.append(path)
        
    with open(output_path, 'wb') as fileobj:
        pdf_merger.write(fileobj)
        
if __name__ == '__main__':
    paths = glob.glob('*.pdf')
    paths.sort()
    merger('data.pdf', paths)



shutil.move(r"data.pdf", r"../data.pdf")

edit:

Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1520.0_x64__qbz5n2kfra8p0\lib\shutil.py", line 788, in move
    os.rename(src, real_dst)
PermissionError: [WinError 32] PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'data.pdf' -> '../data.pdf'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\typ42\Downloads\latextest\merge.py", line 22, in <module>
    shutil.move(r"data.pdf", r"../data.pdf")
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1520.0_x64__qbz5n2kfra8p0\lib\shutil.py", line 803, in move
    os.unlink(src)
PermissionError: [WinError 32] PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'data.pdf'

  [1]: https://stackoverflow.com/questions/27215462/permissionerror-winerror-32-the-process-cannot-access-the-file-because-it-is
  • 2
    Please post the full traceback message. – tdelaney Aug 27 '20 at 21:14
  • 1
    Never worked with this library, but according to the [documentation](https://pythonhosted.org/PyPDF2/PdfFileMerger.html#PyPDF2.PdfFileMerger.close) you should call `pdf_merger.close()`. – Matthias Aug 27 '20 at 21:16
  • @Matthias: Yes, i inserted it after "merger(...." and Traceback (most recent call last): File "C:\Users\typ42\Downloads\latextest\merge.py", line 20, in pdf_merger.close() NameError: name 'pdf_merger' is not defined – user2809537 Aug 27 '20 at 23:26
  • `pdf_merger` is out of scope outside of the function `merger`. You have to call `pdf_merger.close()` in the function. – Matthias Aug 28 '20 at 05:17

0 Answers0