简体   繁体   中英

can't delete a file after no space left on device

I'm writing a bunch of files on several hard disks. All my files don't fit on a single hard drive so I write those on next one if the first one is out of space. I catch the IOError 28 to figure this out.

My exact problem is that when I try to remove the last file written (incomplete file) to the first disk I get a new Exception that I don't fully understand. It seems that with-block can't close the file because there is no space left on a disk.

I'm on windows and disks are formatted to NTFS.

Could someone please help me.

# Here's a sample code
# I recommend first to fill a disk to almost full with a large dummy file.
# On windows you could create a dummy file with
#   'fsutil file createnew large.txt 1000067000000'

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)

Here's the 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'

Answering to my own question.

I filed a bug to python [1][2]. It was already fixed in 3.3+. There is no fix for 3.2 that I used. I upgraded my python version so I'm not suffering this problem anymore.

[1] http://bugs.python.org/issue25202

[2] http://bugs.python.org/issue16597

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM