import re
import subprocess
_handle_pat = re.compile(r'(.*?)\s+pid:\s+(\d+).*[0-9a-fA-F]+:\s+(.*)')
def open_files(name):
"""return a list of (process_name, pid, filename) tuples for
open files matching the given name."""
lines = subprocess.check_output('handle.exe "%s"' % name).splitlines()
results = (_handle_pat.match(line.decode('mbcs')) for line in lines)
return [m.groups() for m in results if m]
您可以使用Microsoft的handle.exe命令行实用程序。例如:
请注意,这对Unicode文件名有限制。在python2中,子进程将
name
作为ANSI字符串传递,因为它调用CreateProcessA
,而不是CreateProcessW
。在python3中,名称作为Unicode传递。无论哪种情况,句柄.exe使用有损ANSI编码写入其输出,因此结果元组中匹配的文件名可能包含最佳匹配字符和“?”替代品。你知道吗相关问题 更多 >
编程相关推荐