我正在几个硬盘上写一堆文件。我所有的文件都不适合一个硬盘,所以如果第一个硬盘空间不足,我会把它们写在下一个硬盘上。我抓住IOError 28来解决这个问题。
我的确切问题是,当我试图删除最后一个写入第一个磁盘的文件(不完整的文件)时,我会得到一个新的异常,我不完全理解。似乎with block无法关闭文件,因为磁盘上没有剩余空间。
我在windows上,磁盘格式化为NTFS。
有人能帮帮我吗。

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

这是回溯:
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'

最佳答案

回答我自己的问题。
我向python[1][2]提交了一个bug。它已经在3.3+中修复。我用的3.2是没有修正的。我升级了python版本,这样我就不会再遇到这个问题了。
[1]http://bugs.python.org/issue25202
[2]http://bugs.python.org/issue16597

关于python - 设备上没有剩余空间后无法删除文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32690018/

10-11 22:24
查看更多