I made a script that converts pdf files into jpgs and then puts those jpgs in a specific folder. The script works fine so far but, I keep getting an error in the VScode terminal that says :
JavaScript
x
2
1
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:/Users/xxx/Desktop/pdf2jpg/src_files/Files/'
2
I tried closing the file before running but, it still does it
JavaScript
1
36
36
1
from pdf2image import convert_from_path
2
import glob, os
3
import os, subprocess
4
import shutil
5
6
pdfPath = "C:/Users/xxx/Desktop/pdf2jpg/src_files/Files/"
7
os.chdir(pdfPath)
8
9
for pdf_file in glob.glob(os.path.join(pdfPath, "*.pdf")): #converts PDF files from pdfPath then
10
#converts
11
pages = convert_from_path(pdf_file, 500)
12
for page in pages:
13
page.save(pdf_file[:-4] +".jpg", 'JPEG')
14
15
for i in os.listdir(pdfPath): #removes orginal PDF files
16
if i.endswith('.pdf'):
17
os.remove(i)
18
19
jpgPath = "C:/Users/xxx/Desktop/pdf2jpg/ConvertedFilesJPG/"
20
21
fileNum = 0
22
foldNum = 0
23
for base, dirs, files in os.walk(jpgPath): #checks number of folders in the destination path
24
for directories in dirs:
25
foldNum += 1
26
for Files in files:
27
fileNum += 1
28
print(foldNum) #for debugging
29
30
if os.path.exists(jpgPath):
31
strNum = foldNum + 1
32
fStrNum = str(strNum)
33
strJpgPath = jpgPath + "Files" + fStrNum + "/" #adds number of files in path, so new folders can
34
#be organized by name
35
shutil.move(pdfPath, strJpgPath)
36
JavaScript
1
21
21
1
Traceback (most recent call last):
2
File "C:UsersxxxAppDataLocalProgramsPythonPython39libshutil.py", line 806, in move
3
os.rename(src, real_dst)
4
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:/Users/xxx/Desktop/pdf2jpg/src_files/Files/' -> 'C:/Users/xxx/Desktop/pdf2jpg/ConvertedFilesJPG/Files4/'
5
6
During handling of the above exception, another exception occurred:
7
8
Traceback (most recent call last):
9
File "c:UsersxxxDesktoppdf2jpgsrc_filesscript.py", line 33, in <module>
10
shutil.move(pdfPath, strJpgPath)
11
File "C:UsersxxxAppDataLocalProgramsPythonPython39libshutil.py", line 824, in move
12
rmtree(src)
13
File "C:UsersxxxAppDataLocalProgramsPythonPython39libshutil.py", line 740, in rmtree
14
return _rmtree_unsafe(path, onerror)
15
File "C:UsersxxxAppDataLocalProgramsPythonPython39libshutil.py", line 622, in _rmtree_unsafe
16
onerror(os.rmdir, path, sys.exc_info())
17
File "C:UsersxxxAppDataLocalProgramsPythonPython39libshutil.py", line 620, in _rmtree_unsafe
18
os.rmdir(path)
19
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:/Users/xxx/Desktop/pdf2jpg/src_files/Files/'
20
PS C:UsersxxxDesktoppdf2jpg>
21
Advertisement
Answer
Your script starts by doing
JavaScript
1
2
1
os.chdir(pdfPath)
2
so the process running your script is the one holding the pdfPath
directory, and preventing it from being removed by shutil.move
. Just do a chdir
somewhere else before moving it, for example:
JavaScript
1
4
1
chdir("C:/Users/xxx/Desktop/pdf2jpg/")
2
if os.path.exists(jpgPath):
3
4