无法删除文件 - 另一个进程正在使用的文件

时间:2015-06-29 13:00:30

标签: python python-imaging-library pillow

我目前使用Pillow(和windows)lib将两种类型的文件转换为jpeg。问题是我创建了一个tmp文件来改变(裁剪/重新调整大小/旋转/等),但后来我无法删除它。 如果文件是X类型,则可以删除,如果不是X类型,则会出错。 这两个文件类型的过程是一样的但是我在删除没有X类型的文件时遇到错误。 已经尝试强制fout.close(),即使"与#34;声明默认执行。

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

只有一个程序实例在运行,文件/目录中没有并发,写权限没有问题,看起来我已经关闭了所有描述符。

        #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'

1 个答案:

答案 0 :(得分:1)

这可能与这一行有关:

im = tmp = PIL.Image.open(outputfile)

实际上并没有打开图像的两个副本。相反,您可能需要以下内容:

im = PIL.Image.open(outputfile)
tmp = im.copy()