0

I am getting the error while deleting the files in folder.
Below is my code.

Part-1 of coding

pdf = FPDF()
sdir = "D:/IMAGES/"
w, h = 0, 0
for i in range(1, 25):
 fname = (sdir + str(i) + ".jpeg")
 if os.path.exists(fname):
 if i == 1:
 cover = Image.open(fname)
 w, h = cover.size
 pdf = FPDF(unit="pt", format=[w, h])
 image = fname
 pdf.add_page()
 pdf.image(image, 0, 0, w, h)
 else:
 pdf.output(
 r"D:\DOCUMENTS\Google Drive\NewsPapers\Lokmat\Lokmat Mumbai Main "+str(d+A+Y)+".pdf", "F")
 pdf.close

Part-2 of coding

import os
dir_name = "D:/IMAGES/"
test = os.listdir(dir_name)
for item in test:
 if item.endswith(".jpeg"):
 os.remove(os.path.join(dir_name, item))
 print("Done")
print("--- %s seconds ---" % (time.time() - start_time))

Error I am getting is as below:

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'D:/IMAGES/1.jpeg'
asked Apr 29, 2020 at 16:18
4
  • 1
    Have you closed all the processes that use the file before you tried to delete it? Commented Apr 29, 2020 at 16:19
  • @RulerOfJustice I am new to python and I dont know how to close all processes. Commented Apr 29, 2020 at 16:21
  • To put the question differently - have you got file "1.jpeg" opened? Commented Apr 29, 2020 at 16:22
  • @0buz No. Even I have restated the PC also. Still getting this error. I am updating my full code. Please see and help. Commented Apr 29, 2020 at 16:25

3 Answers 3

1

You forgot to write

cover.close()

After the line:

pdf = FPDF(unit="pt", format=[w, h])

Because of that, you still have an opened file and you cannot delete it.

answered Apr 29, 2020 at 16:37
7
  • Unfortunately it doesn't help :( Commented Apr 29, 2020 at 17:03
  • @AkshayPoklekar Well it works fine to me using python 3.8.2 , what errors are you getting when you use 0buz's or my answer? Commented Apr 29, 2020 at 17:06
  • I am also using python 3.82. And getting the error: PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'D:/IMAGES/1.jpeg' Commented Apr 29, 2020 at 17:07
  • @AkshayPoklekar Could you please update the code in the post so we will see the most 'fresh' code you're struggling with? Commented Apr 29, 2020 at 17:09
  • 1
    @AkshayPoklekar - you should accept this answer if you are happy with the input. After all it did state cover.close(), not cover.close. Commented Apr 29, 2020 at 18:01
1

Try replacing this:

cover = Image.open(fname)
w, h = cover.size
pdf = FPDF(unit="pt", format=[w, h])

With:

with Image.open(fname) as cover:
 w, h = cover.size
 pdf = FPDF(unit="pt", format=[w, h])

Using with should help with situations where you may forget to close the file once you're done using it.

answered Apr 29, 2020 at 16:38
1
  • There are too many errors which I cant include in comments also :D Commented Apr 29, 2020 at 16:59
0

My problem is solved now :)

Actually I used

cover.close()

insted of

cover.close

And I used this code before line

pdf.output(r"D:\DOCUMENTS\Google Drive\NewsPapers\Lokmat\Lokmat Mumbai Main "+str(d+A+Y)+".pdf", "F")
answered Apr 29, 2020 at 17:17

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.