python open文件被另一个进程打开怎么办,无法删除文件-文件正在被另一个进程使用...

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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值