gpt4 book ai didi

Windows Server 上的 Python - 获取用户访问共享文件

转载 作者:行者123 更新时间:2023-12-01 05:58:58 25 4
gpt4 key购买 nike

我需要查找哪个进程、用户或电脑打开了某个文件。我可以通过 win 服务器附带的实用程序手动完成此操作,但我想编写脚本,因为我正在处理数百个文件。

有什么想法、建议吗?

基本上是以下信息的列表: enter image description here

最佳答案

要获取此信息需要一些未记录的 Win32 API。您可以调用类似 handle.exe来自使用它们的 sysinternals,但我建议您使用 PyPi 提供的 psutil 模块。这是我碰巧拥有的一些示例代码(Py3),其中列出了正在使用的所有文件,您应该能够对其进行修改:

import psutil

for proc in psutil.process_iter():
try:
flist = proc.get_open_files()
if flist:
print(proc.pid,proc.name)
for nt in flist:
print("\t",nt.path)

except psutil.NoSuchProcess as err:
print("****",err)

请注意,此类查询总是有可能出现竞争条件 - 您不知道进程在什么时候打开或关闭文件,甚至不知道开始和结束。

PS:如果您使用的是 Python 3,我在安装 psutil 时遇到了一些问题,但在 setup.py 上使用 2to3 转换实用程序解决了这些问题。

关于Windows Server 上的 Python - 获取用户访问共享文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11359713/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com