无法删除文件-文件正在被另一个进程使用 [英] Unable to delete file - File being used by another process

查看:310
本文介绍了无法删除文件-文件正在被另一个进程使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用Pillow(和Windows)库将2种类型的文件转换为jpeg.问题是我创建了一个tmp文件来更改(裁剪/调整大小/旋转/等),但后言我无法将其删除. 如果文件是X类型,则可以将其删除;如果不是X类型,则将给出错误. 两种文件类型的处理过程都相同,但是删除非X类型的文件时却出现错误. 即使默认情况下"with"语句也已尝试强制fout.close().

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.

如果我在其上设置了try/except语句,我知道它可以被处理,但是该文件不会被删除.

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)

我得到的错误:

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'

推荐答案

它可能与此行有关:

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

这篇关于无法删除文件-文件正在被另一个进程使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆