I currently use the Pillow (and windows) lib to convert 2 types of files to jpeg. The problem is I create a tmp file to alter (crop/re-size/rotate/etc) but afterwords I cannot delete it.
If the file is X type it can be deleted, if is not X type it will give an error.
The process is the same on both file types yet I do get an error on deleting a file which is no X type.
Already trying forcing fout.close() even though "with" statement does it by default.
If I set a try/except statement on the I know it can be dealt with, but the file wont be deleted.
There is only one instance of the program running, there is no concurrency in the files/directory, there is no problem with write permissions, and looks like I've closed all descriptors.
#If the file is X TYPE
if is X:
# Search for X TYPE file header and store index
index = data.find(X_FILE_HEADER)
# Only process file containing X otherwise return
if index == -1:
self.my_logger.error(
'Could not find X signature on file "%s", ' % inputfile)
return
try:
outputfile += '.X'
with open(outputfile, 'wb') as fout:
fout.write(data[index:])
except:
self.my_logger.critical('Could not create file "%s"' % outputfile)
return
# Not X file type
else:
try:
with open(outputfile, 'wb') as fout:
fout.write(data)
except:
self.my_logger.critical('Could not create file "%s"' % outputfile)
return
# Check if chart name in conf file
for chart in self.chart_list:
if os.path.basename(outputfile).startswith(chart.name):
if isX:
tmp_chart_name = outputfile.replace(".x",".jpeg")
else:
tmp_chart_name = outputfile.replace(".z",".jpeg")
# Tmp for legend crop box usage
im = tmp = PIL.Image.open(outputfile)
# The output file wont have any timestamp
outputfile_jpeg = os.path.join(os.path.dirname(outputfile),tmp_chart_name)
# Check if rotation needed
if rotation:
im = im.rotate(float(rotation))
# Check if crop needed
if crop_box:
box = tuple(map(int, crop_box.split(',')))
im = im.crop(box)
im.copy()
# Check if legend crop/relocate needed
if legend_crop_box:
box = tuple(map(int, legend_crop_box.split(',')))
legend_box = tmp.crop(box)
im.paste(legend_box, (0, 0))
# Convert the image
im.convert('RGB').save(outputfile_jpeg)
im.close()
tmp.close()
# Delete png file - Where is where the problem/bug presists
if os.path.exists(outputfile):
os.remove(outputfile)
The error I get:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1532, in __call__
return self.func(*args)
File "getmail.py", line 512, in get_email
self.deamon.process_email()
File "getmail.py", line 175, in process_email
os.path.join(self.modifieddir, filename))
File "getmail.py", line 302, in convert_file
os.remove(outputfile)
WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'C:\\FILE_TRUNCADED_PATH\\my_file.x'
解决方案
It may be related to this line:
im = tmp = PIL.Image.open(outputfile)
which does not actually open two copies of the image. Instead, you might want something like:
im = PIL.Image.open(outputfile)
tmp = im.copy()