windows下python檢查文件是否被其它文件打開.md
有時候我們需要能夠判斷一個文件是否正在被其它文件訪問,幾乎不可避免的要調用操作系統接口
from ctypes import cdll import os _sopen = cdll.msvcrt._sopen _close = cdll.msvcrt._close _SH_DENYRW = 0x10 def is_open(filename): if not os.access(filename, os.F_OK): return False # file doesn't exist h = _sopen(filename, 0, _SH_DENYRW, 0) if h == 3: _close(h) return False # file is not opened by anyone else return True # file is already open print is_open("test.txt")