Reputation: 3
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'
Upvotes: 0
Views: 963
Reputation: 3
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")
Upvotes: 0
Reputation: 3503
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.
Upvotes: 1
Reputation: 1842
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.
Upvotes: 1