3
我在幾個硬盤上寫了一堆文件。我的所有文件都不適合單個硬盤驅動器,因此如果第一個文件空間不足,我會將它們寫入下一個文件中。我發現了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'
我認爲這種錯誤'WindowsError:[錯誤32]的方法,因爲它被另一個process'不能訪問該文件是很清楚的,並且指定原因。 –
是不是因爲上面的代碼在試圖刪除它之前沒有關閉文件而發生錯誤(32)? – nekomatic
@ρss對我來說不是很清楚。這個文件如何被另一個進程使用,因爲它在寫入時(在Windows上)是開放的?如果這個「另一個進程」實際上是同一個進程,那麼問題是爲什麼'with'語句沒有正確關閉文件。文件對象的上下文管理器保證文件將被關閉,並釋放OS級資源,即使發生異常。 – user4815162342