pdf2image icon indicating copy to clipboard operation
pdf2image copied to clipboard

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process

Open jamarshon opened this issue 5 years ago • 8 comments

Describe the bug I get the error PermissionError: [WinError 32] The process cannot access the file because it is being used by another process

To Reproduce Steps to reproduce the behavior:

  1. pip install pdf2image
  2. Run the following code
with tempfile.TemporaryDirectory() as path:
    images_from_path = convert_from_path(data_path, output_folder=path)
  1. See error
Traceback (most recent call last):
  File "...\lib\tempfile.py", line 803, in onerror
    _os.unlink(path)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: '...\\AppData\\Local\\Temp\\tmppglie128\\ad47d7e6-b50f-4bb9-ad12-533e4e25b4a9-001.ppm'

Desktop (please complete the following information):

  • OS: Windows
  • Version 1.13.1

jamarshon avatar Jul 22 '20 23:07 jamarshon

https://github.com/Belval/pdf2image/blob/e0bf64d6ebaf9b4545379679c7f911d1917ad9e7/pdf2image/pdf2image.py#L471 I believe it is an issue with this line. If I run with paths_only, there is no issue

with tempfile.TemporaryDirectory() as path:
    images_from_path = convert_from_path(data_path, output_folder=path, paths_only=True)

PIL Image.open seems to require close() which is not being called with the context manager causing the temporary file to be not deletable in a separate process. A solution could be to more explicit in the example snippet in README.md that the images have to be closed.

jamarshon avatar Jul 23 '20 01:07 jamarshon

Hmm this is a problematic one because I can't seem to reproduce the issue on Linux. I will try to reproduce it on Windows.

If I successfully reproduce the issue on my machine I will update the README.md, Is there a snippet that you think woud be a suitable replacement?

Unfortunately the one with paths_only=True isn't really a good solution as it will cause the underlying images to be deleted on the context exit which in the turn will raise more issues when the user tries to open them.

Belval avatar Jul 29 '20 00:07 Belval

Maybe this code snippet would work. I tried this locally and this version does not have the error.

import tempfile

with tempfile.TemporaryDirectory() as path:
    images_from_path = convert_from_path('/home/belval/example.pdf', output_folder=path)
    # Do something here
    # ...
    for image in images_from_path:
        image.close()  # Close the image once we are done with it

jamarshon avatar Jul 31 '20 08:07 jamarshon

@Belval I had the exact same problem yesterday, tried @jamarshon solution and it solved it. What could it be?

marcio-pg avatar Oct 14 '20 18:10 marcio-pg

When you exit the context, the underlying image files are open in your Python process. This causes an issue because the temporary directory deletion cannot take place on Windows as it contains open files.

If you don't use a temporary directory you won't have this issue.

Belval avatar Oct 15 '20 14:10 Belval

You have made an error in a place where the error is hidden from you.

mohsenhrt avatar Nov 24 '21 16:11 mohsenhrt

@Belval i am not using temporary directory but still getting this issue

here is my code :-- image

i am just creating the folder and downloading medias to it then reuploading it then want to delete that folder but it is showing this err

image

dheeraj009joshi avatar Mar 19 '22 14:03 dheeraj009joshi