但是我得到了WindowsError
。编辑完文件后,我已将其关闭。如何检查访问文件的其他进程。
C:\Documents and Settings\Administrator>python
Python 2.6.1 (r261:67517, Dec 4 2008, 16:51:00) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tempfile
>>> __, filename = tempfile.mkstemp()
>>> print filename
c:\docume~1\admini~1\locals~1\temp\tmpm5clkb
>>> fptr = open(filename, "wb")
>>> fptr.write("Hello World!")
>>> fptr.close()
>>> import os
>>> os.remove(filename)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
WindowsError: [Error 32] The process cannot access the file because it is being used by
another process: 'c:\\docume~1\\admini~1\\locals~1\\temp\\tmpm5clkb'
文件仍处于打开状态。执行以下操作:
从documentation:
因此,
mkstemp
同时将OS文件句柄返回到临时文件的文件名和。重新打开临时文件时,原始返回的文件句柄仍处于打开状态(没有人阻止您在程序中打开两个或多个相同的文件)。如果要将该OS文件句柄作为python文件对象进行操作,可以:
然后继续你的正常代码。
我相信您需要释放fptr才能干净地关闭文件。尝试将fptr设置为None。
相关问题 更多 >
编程相关推荐