我已经查看了Python:检查文件是否被锁定和如何在Python中检查文件是否打开及其打开状态。
通常,以下代码适用于我的需要,但是对于Unicode字符串,它在Python 2中无法正常工作。
from ctypes import cdll
_sopen = cdll.msvcrt._sopen
_close = cdll.msvcrt._close
_SH_DENYRW = 0x10
def is_open(filename):
h = _sopen(filename, 0, _SH_DENYRW, 0)
try:
return h == -1
finally:
_close(h)
您有什么建议应该做什么?
_sopen
调用了哪个*A()
("ANSI")win32 API 函数,然后调用相应的*W
(宽字符)函数来处理 Unicode 字符串。 - jfs