模块需要运行两次才能产生结果

时间:2016-01-10 09:58:25

标签: python process subprocess

我正在尝试显示系统的输出。但是,我的脚本仅在我运行两次时生成结果。下面是脚本。在这两个地方使用subprocess.Popen不会产生任何输出,与subprocess.call相同。

#!/usr/bin/env python
import subprocess
import re
contr = 0
spofchk='su - dasd -c "java -jar /fisc/dasd/bin/srmclient.jar -spof_chk"'
res22 = subprocess.call("touch /tmp/logfile",shell=True,stdout=subprocess.PIPE)
fp = open("/tmp/logfile","r+")
res6 =subprocess.Popen(spofchk,shell=True,stdout=fp)
fil_list=[]
for line in fp:
    line = line.strip()
    fil_list.append(line)
fp.close()
for i in fil_list[2:]:
        if contr % 2 == 0:
            if 'no  SPOF' in i:
                flag=0
                #print(flag)
                #print(i)
            else:
                flag = 1

        else:
            continue
        #Incrementing the counter by 2 so that we will only read line with spof and no SPOF
        contr+=2

2 个答案:

答案 0 :(得分:0)

子进程有自己的文件描述符,因此您可以在子进程启动后立即关闭父进程中的文件。

阅读整个儿童过程'重定向到文件的输出,等待它退出:

library(dplyr)
df1 %>%
    group_by(ID1) %>%
    summarise(S.no = S.no[1L], Count=n(), ID2= paste(ID2, collapse=' '))

如果要在子进程运行时使用输出,请使用import subprocess with open('logfile', 'wb', 0) as file: subprocess.check_call(command, stdout=file) with open('logfile') as file: # read file here...

PIPE

这里是version for older Python versions and links to fix other possible issues

答案 1 :(得分:-1)

由于子进程打开一个新shell,所以在第一次创建文件和文件并同时写入另一个子进程的输出时是不可能的  ..因此,唯一的解决方案是使用操作系统。系统..

相关问题