python 判断文件是否打开、如果打开先关闭_如果使用打开文件,如何知道文件是否已关闭子流程。Popen?...

你要找的是这样的东西:from __future__ import print_function # If in Python 2.x, import print function from Python 3.x

import os

import subprocess

import time

import psutil

def get_proc_status(pid):

"""Get the status of the process which has the specified process id."""

proc_status = None

try:

proc_status = psutil.Process(pid).status()

except psutil.NoSuchProcess as no_proc_exc:

print(no_proc_exc)

except psutil.ZombieProcess as zombie_proc_exc:

# For Python 3.0+ in Linux (and MacOS?).

print(zombie_proc_exc)

return proc_status

def open_file(app, file_path):

"""Open the specified file with the specified app, and wait for the file to be closed. """

my_file = os.path.normpath(file_path)

# phandler = subprocess.Popen(['open', '-W', my_file])

# Must be for Macs. Doesn't work in Windows or Linux.

phandler = subprocess.Popen([app, my_file])

pid = phandler.pid

pstatus = get_proc_status(pid)

while pstatus is not None and pstatus != "zombie":

# The allowed set of statuses might differ on your system.

print("subprocess %s, current process status: %s." % (pid, pstatus))

time.sleep(1) # Wait 1 second.

pstatus = get_proc_status(pid)

# Get process status to check in 'while' clause at start of next loop iteration.

if os.name == 'posix' and pstatus == "zombie": # Handle zombie processes in Linux (and MacOS?).

print("subprocess %s, near-final process status: %s." % (pid, pstatus))

phandler.communicate()

pstatus = get_proc_status(pid)

print("subprocess %s, final process status: %s.\n" % (pid, pstatus))

open_file(r"Evince\bin\Evince.exe", "fuzzed_file.pdf")

open_file(r"Evince\bin\Evince.exe", "unfuzzed_file.pdf")

这将产生如下输出:

^{pr2}$

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值