Message251225
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 |
|
Date |
User |
Action |
Args |
2015-09-21 13:17:04 | Lauri Kajan | set | recipients:
+ Lauri Kajan |
2015-09-21 13:17:04 | Lauri Kajan | set | messageid: <1442841424.93.0.271808876405.issue25202@psf.upfronthosting.co.za> |
2015-09-21 13:17:04 | Lauri Kajan | link | issue25202 messages |
2015-09-21 13:17:04 | Lauri Kajan | create | |
|