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'
Akshay PoklekarAkshay Poklekar
asked Apr 29, 2020 at 16:18
-
1Have you closed all the processes that use the file before you tried to delete it?0Interest– 0Interest2020年04月29日 16:19:21 +00:00Commented Apr 29, 2020 at 16:19
-
@RulerOfJustice I am new to python and I dont know how to close all processes.Akshay Poklekar– Akshay Poklekar2020年04月29日 16:21:19 +00:00Commented Apr 29, 2020 at 16:21
-
To put the question differently - have you got file "1.jpeg" opened?0buz– 0buz2020年04月29日 16:22:39 +00:00Commented 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.Akshay Poklekar– Akshay Poklekar2020年04月29日 16:25:10 +00:00Commented Apr 29, 2020 at 16:25
3 Answers 3
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
-
Unfortunately it doesn't help :(Akshay Poklekar– Akshay Poklekar2020年04月29日 17:03:33 +00:00Commented 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?0Interest– 0Interest2020年04月29日 17:06:10 +00:00Commented 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'Akshay Poklekar– Akshay Poklekar2020年04月29日 17:07:28 +00:00Commented 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?0Interest– 0Interest2020年04月29日 17:09:14 +00:00Commented 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()
, notcover.close
.0buz– 0buz2020年04月29日 18:01:38 +00:00Commented Apr 29, 2020 at 18:01
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
-
There are too many errors which I cant include in comments also :DAkshay Poklekar– Akshay Poklekar2020年04月29日 16:59:19 +00:00Commented Apr 29, 2020 at 16:59
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
lang-py