我正在尝试使用以下命令删除临时文件:
os.remove(str('temp.bin'))
下面是完整的函数,请注意,我正在使用一个API(XWF)将数据读取到一个文件中,它可以正常工作。当它收到一个有效的图像时,这个操作很好,所以问题不在这个关闭之外。代码只有在接收到无效的图像文件时才有问题。我将输入读入临时文件,因为它对我的解决方案很有效,这里的问题是当它们不是有效的图像时,它不会删除它们。我不想找一个讲座,为什么它会更好地检测是否是一个图像,然后再写一个临时文件。幽默我,现在我有充分的理由这么做。
import OutputRedirector
import XWF
import os
from PIL import Image
from PIL.ExifTags import TAGS, GPSTAGS
gps_data = {}
def XT_ProcessItemEx(nItem, hItem, reserved):
global gps_data
fn = XWF.GetItemName(nItem)
offset = 0
size = XWF.GetItemSize(nItem)
if offset < size:
tempfn = fn + '.bin'
f = open(tempfn, 'wb')
buffer = XWF.Read(hItem, offset, size)
f.write(buffer)
f.close()
try:
image = Image.open(tempfn)
exif_data = get_exif_data(image)
gps = get_lat_lon(exif_data)
if gps[0]:
gps_data[fn] = (repr(gps[0]), repr(gps[1]))
print('Found GPS data in %s' % fn)
else:
print('No GPS data in image %s' % fn)
del image
os.remove(str(tempfn)) # it works fine here
except IOError:
print('Not an image')
os.remove(str(f)) # but it won't work here
else:
print('File too small')
return
如果不按原样保留该行,并且不使用str()存储模块,则会出现以下错误:
TypeError : must be string, not file
因为我得到这个错误:
WindowsError : [Error 123] The filename, directory name, or volume
label syntax is incorrect: "<closed file u'file.pdf.bin',
mode 'wb' at 0x0A82A0D0>"
如果在返回函数/方法根级别之前直接移动有问题的行,则会出现以下错误:
WindowsError : [Error 32] The process cannot access the file because
it is being used by another process
我不知道为什么它与图像,但不与f
目前没有回答
相关问题 更多 >
编程相关推荐