【发布时间】:2016-03-02 15:42:20
【问题描述】:
我已经签出了Python : Check file is locked 和How to check whether a file is_open and the open_status in 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)
有什么建议吗?
【问题讨论】:
-
文件以 unicode 编码,还是文件名本身是 unicode 字符串?
-
你收到错误了吗?
-
类型(文件名)。文件的编码是什么并不重要。
-
看看
_sopen调用了*A()(“ANSI”)win32 API函数,并为Unicode字符串调用了对应的*W(宽)函数。
标签: python file-io unicode msvcrt