Unable to delete file - File being used by another process

安稳与你 提交于 2019-12-11 12:04:15

问题


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'

回答1:


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


来源:https://stackoverflow.com/questions/31116422/unable-to-delete-file-file-being-used-by-another-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!