I am trying to convert all pdf files to .jpg files and then remove them from the directory. I am able to convert all pdf's to jpg's but when I try to delete them, I get the error "The process is being used by another person".
Could you please help me?
Below is the code
Below script wil convert all pdfs to jpegs and storesin the same location.
for fn in files:
doc = fitz.open(pdffile)
page = doc.loadPage(0) # number of page
pix = page.getPixmap()
fn1 = fn.replace('.pdf', '.jpg')
output = fn1
pix.writePNG(output)
os.remove(fn) # one file at a time.
path = 'D:/python_ml/Machine Learning/New folder/Invoice/'
i = 0
for file in os.listdir(path):
path_to_zip_file = os.path.join(path, folder)
if file.endswith('.pdf'):
os.remove(file)
i += 1
As @K J noted in their comment, most probably the problem is with files not being closed, and indeed your code misses closing the
docobject(s).(Based on the line
fitz.open(pdffile), I guess you use the pymupdf library.)The problematic fragment:
...should be adjusted, e.g., in the following way:
(Side note: the
fn1variable seems to be completely redundant so I got rid of it. Also, shouldn'tpdffilebe replaced withfn? Whatpdffileactually is?)