This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Lauri Kajan
Recipients Lauri Kajan
Date 2015-09-21.13:17:04
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1442841424.93.0.271808876405.issue25202@psf.upfronthosting.co.za>
In-reply-to
Content
Hi all,

I found out that handle to a file opened with with-statement is not released in all cases.

I'm trying to delete a file when space on a disk is filled (0 bit left) by writing this file. I catch the IOError 28 to figure this out.
I write the file within the with-statement that is wrapped with try except. In the except block I try to delete the file but the handle to the file is not released.

In the following code fill-file can't be deleted because the file is still open.
I have described the problem also in stackoverflow [1].

# I recommend filling the disk almost full with some better tool.
import os
import errno

fill = 'J:/fill.txt'
try:
    with open(fill, 'wb') as f:
        while True:
            n = f.write(b"\0")
except IOError as e:
    if e.errno == errno.ENOSPC:
        os.remove(fill)

This gives the following traceback:

Traceback (most recent call last):
  File "nospacelef.py", line 8, in <module>
    n = f.write(b"\0")
IOError: [Errno 28] No space left on device

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "nospacelef.py", line 8, in <module>
    n = f.write(b"\0")
IOError: [Errno 28] No space left on device

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "nospacelef.py", line 11, in <module>
    os.remove(fill)
WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'J:/fill.txt'



[1] http://stackoverflow.com/questions/32690018/cant-delete-a-file-after-no-space-left-on-device
History
Date User Action Args
2015-09-21 13:17:04Lauri Kajansetrecipients: + Lauri Kajan
2015-09-21 13:17:04Lauri Kajansetmessageid: <1442841424.93.0.271808876405.issue25202@psf.upfronthosting.co.za>
2015-09-21 13:17:04Lauri Kajanlinkissue25202 messages
2015-09-21 13:17:04Lauri Kajancreate