Reals
Reals

Reputation: 3

Python WinError 32 The process cannot access the file because it is being used by another process

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 :

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/'

I tried closing the file before running but, it still does it

from pdf2image import convert_from_path
import glob, os
import os, subprocess
import shutil

pdfPath = "C:/Users/xxx/Desktop/pdf2jpg/src_files/Files/"
os.chdir(pdfPath)

for pdf_file in glob.glob(os.path.join(pdfPath, "*.pdf")): #converts PDF files from pdfPath then 
#converts
pages = convert_from_path(pdf_file, 500)   
for page in pages:       
    page.save(pdf_file[:-4] +".jpg", 'JPEG')

for i in os.listdir(pdfPath): #removes orginal PDF files
    if i.endswith('.pdf'):
        os.remove(i)

jpgPath = "C:/Users/xxx/Desktop/pdf2jpg/ConvertedFilesJPG/"

fileNum = 0
foldNum = 0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
for base, dirs, files in os.walk(jpgPath): #checks number of folders in the destination path   
for directories in dirs:
    foldNum += 1                       
for Files in files:
    fileNum += 1
print(foldNum) #for debugging

if os.path.exists(jpgPath): 
    strNum = foldNum + 1
    fStrNum = str(strNum)
    strJpgPath = jpgPath + "Files" + fStrNum + "/"  #adds number of files in path, so new folders can 
    #be organized by name 
    shutil.move(pdfPath, strJpgPath)
Traceback (most recent call last):
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python39\lib\shutil.py", line 806, in move
    os.rename(src, real_dst)
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/'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\Users\xxx\Desktop\pdf2jpg\src_files\script.py", line 33, in <module>
    shutil.move(pdfPath, strJpgPath)
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python39\lib\shutil.py", line 824, in move
    rmtree(src)
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python39\lib\shutil.py", line 740, in rmtree
    return _rmtree_unsafe(path, onerror)
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python39\lib\shutil.py", line 622, in _rmtree_unsafe
    onerror(os.rmdir, path, sys.exc_info())
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python39\lib\shutil.py", line 620, in _rmtree_unsafe
    os.rmdir(path)
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/'
PS C:\Users\xxx\Desktop\pdf2jpg>

Upvotes: 0

Views: 12909

Answers (1)

joao
joao

Reputation: 2293

Your script starts by doing

os.chdir(pdfPath)

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:

chdir("C:/Users/xxx/Desktop/pdf2jpg/")
if os.path.exists(jpgPath):
    ...

Upvotes: 1

Related Questions