gpt4 book ai didi

python - 杀死所有锁定文件的进程

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

当我运行 Python 脚本时,有时会收到以下错误:

"the process cannot access the file, because it is being used by another process"

现在我想知道:python 有没有办法:

  1. 检测哪个进程正在使用该文件?
  2. 关闭此进程? (例如使用 os.system('taskkill /f /im PROCESS.exe) )

最佳答案

如果您与 psutil 所需的文件匹配,您可以尝试迭代进程并终止它:

import psutil


for p in psutil.process_iter():
try:
if "filename" in str(p.open_files()):
print(p.name())
print("^^^^^^^^^^^^^^^^^")
p.kill()
except:
continue

关于python - 杀死所有锁定文件的进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56947481/

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