2015-09-21 38 views
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' 
+3

我认为这种错误'WindowsError:[错误32]的方法,因为它被另一个process'不能访问该文件是很清楚的,并且指定原因。 –

+0

是不是因为上面的代码在试图删除它之前没有关闭文件而发生错误(32)? – nekomatic

+2

@ρss对我来说不是很清楚。这个文件如何被另一个进程使用,因为它在写入时(在Windows上)是开放的?如果这个“另一个进程”实际上是同一个进程,那么问题是为什么'with'语句没有正确关闭文件。文件对象的上下文管理器保证文件将被关闭,并释放OS级资源,即使发生异常。 – user4815162342

回答

相关问题