PermissionError: [WinError 32] The process cannot access the file because it is being used by another process
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:
- pip install pdf2image
- Run the following code
with tempfile.TemporaryDirectory() as path:
images_from_path = convert_from_path(data_path, output_folder=path)
- 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
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.
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.
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
@Belval I had the exact same problem yesterday, tried @jamarshon solution and it solved it. What could it be?
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.
You have made an error in a place where the error is hidden from you.
@Belval i am not using temporary directory but still getting this issue
here is my code :--

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
