将popen输出转换为列表,并执行操作

2024-10-05 22:28:50 发布

您现在位置:Python中文网/ 问答频道 /正文

def calc_execution():
    import subprocess
    get_pid_detectmotion = "pgrep -f detectmotion.py"
    pidcmd = subprocess.Popen(get_pid_detectmotion.split(), stdout=subprocess.PIPE)
    pidcmd, error = pidcmd.communicate()
    #print pidcmd
    #detectmotion_file_pid = int(out.rstrip())
    get_length_pid_running="ps -o etime= -p" + pidcmd
    length_pid_detectmotion_running = subprocess.Popen(get_length_pid_running.split())#, int(pidcmd))
    print length_pid_detectmotion_running
    print list(length_pid_detectmotion_running)

输出:

^{pr2}$

如何将length_pid_detectmotion_running的输出转换为list,然后获得最接近左边的值,如果有(3)。例如:23:15:59我想在一个类似length_pid_detectmotion_running[0]的列表中打印出23


Tags: getdefcalcpidlengthrunninglistint
1条回答
网友
1楼 · 发布于 2024-10-05 22:28:50

当您想执行一些并行任务时,Popen非常有用,比如在程序运行时逐行控制/修改打印,除了输出

Popen是一个结构,不可直接写入。要获得进程标准输出的行列表,应该将length_pid_detectmotion_running.stdout转换为list。在

但在您的例子中,您应该使用check_output并拆分:

output = subprocess.check_output(get_length_pid_running.split())
toks = output.split(":")

toks的第一个元素应该是您的23

相关问题 更多 >