file - 在 Python 中,如何关闭使用 os.startfile() 打开的文件?
问题描述
我使用 os.startfile('test-conversion.pdf') 打开 PDF 文件,但我想关闭文件和打开它的应用程序。os.startfile() 启动关联的应用程序,但可能会使用 Windows 注册表来执行此操作。
如何以编程方式找出哪个应用程序打开了它,获取它的进程句柄,然后关闭应用程序和文件?
import os
import subprocess
# specify the file name
file1 = 'test-conversion.pdf'
# open the file using the .startfile() method
os.startfile('test-conversion.pdf')
# open the file using the subprocess.Popen() method
subprocess.Popen([file1],shell=True)
# subprocess.Pclose([file1],shell=True)
我尝试了最后一行,但它会引发错误并且不会关闭文件或进程。
解决方案
推荐阅读
- javascript - 将参数传递给匿名函数Javascript
- http - 可以将 GitHub README 重定向到另一个位置吗?
- r - How to properly set a file path based on a yearly-date basis in R?
- haskell - 这是一个有效的单子实例吗?
- python - 不使用 map 方法将列表列表从字符串转换为整数
- flutter - 如何使用 Flutter 从 IP 地址流式传输实时视频?
- time-series - 在时间序列分类问题中规范化数据 - 递归神经网络
- splunk - Stats UI 和 Splunk API 不匹配
- python - Tkinter 缩小小部件之间的差距
- javascript - 内存已满时删除最旧的 chrome.storage.sync?