subprocess.Popen用于循环输出

时间:2017-09-14 10:57:27

标签: python subprocess

代码:

with open ('Client.txt','r') as Client_Name:  
    for Client in Client_Name:  
        out = subprocess.Popen(['script.sh', '-byclient', Client], stdout=subprocess.PIPE)   
        outputstring = out.communicate()  
        print (outputstring)

此代码未提供输出,我想将Client.txt文件中的客户端名称传递给script.sh。请提出任何建议。

1 个答案:

答案 0 :(得分:0)

将您的名称传递给脚本的代码似乎是正确的,但E:\Work\test>elementor https://angularjs.org Creating protractor configuration file Starting protractor Running command: [node C:\Users\Vikash777\AppData\Roaming\npm\node_modules\elementor\node_modules\protractor\built\cli.js --elementExplorer true --debuggerServerPort 6969 C:\Users\VIKASH~1\AppData\Local\Temp\protractor-conf117814-7776-q2aq5m.jdozu84cxr] E:\Work\test> 实际上会返回communicatetuple

您可能还想将(stdout_data, stderr_data)传递给universal_newlines=True来解码输出。试试这个:

Popen

或者,如果您想实时打印输出:

out = subprocess.Popen(['script.sh', '-byclient', Client],
                       stdout=subprocess.PIPE,
                       universal_newlines=True)
out.wait()  # wait until the script has finished
stdout_data, stderr_data = out.communicate()
print(stdout_data)