python - 是否可以使用 Python 检查是否已在 Windows 上打开文件夹?
问题描述
例如,假设我的桌面上有一个文件夹。当我打开文件夹时,程序是否可以在它发生时运行?有什么方法可以识别特定文件夹打开的动作吗?
解决方案
你可以使用handle.exe
微软发布的,可以从微软官方网站下载:https ://docs.microsoft.com/zh-cn/sysinternals/downloads/handle
下载后,放入handle.exe
同一个文件夹test.py
,然后执行python test.py $your_folder
得到结果,仅供参考。
测试.py
import os
import sys
candidate_folder = sys.argv[1]
print('Please wait for some seconds before the process done...')
rc = os.system("handle.exe -accepteula %s | findstr pid" %(candidate_folder))
if 0 == rc:
print('The folder is opened by above process.')
else:
print('No process open this folder now.')
1)test1:(不要打开文件夹C:\abc)
C:\Handle>python test.py C:\abc
Please wait for some seconds before the process done...
No process open this folder now.
2)test2:(打开文件夹C:\abc)
C:\Handle>python test.py C:\abc
Please wait for some seconds before the process done...
explorer.exe pid: 15052 type: File 2B9C: C:\abc
explorer.exe pid: 15052 type: File 2BC0: C:\abc
The folder is opened by above process.
推荐阅读
- python - 这是我试图在 Jupyter 上运行的一个简单代码,它来了
- turtle-graphics - 运行乌龟代码时没有创建新窗口
- docker - Nginx 代理传递到 dockerized gunicorn 服务器
- c++ - 带有 if 语句的函数没有返回值
- testing - Testcafe - 锤头阴影 UI 的加载图标可见
- python - 我想做 django 聊天应用程序
- powershell - 创建一个具有 3 个标题的对象:用户、ID、时间
- flutter - Flutter 使用视频播放器或 ffmpeg 获取视频帧
- python - return 语句没有响应我该怎么办
- machine-learning - 一级与两级目标检测