我正在几个硬盘上写一堆文件。我的所有文件都不适合单个硬盘驱动器,所以如果第一个文件没有空间,我会在下一个文件上写下这些文件。我抓住了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'
答案 0 :(得分:1)
回答我自己的问题。
我向python [1] [2]提交了一个错误。它已在3.3+中修复。我使用的3.2没有修复。我升级了我的python版本,所以我不再遇到这个问题了。