Unable to store terminal output of subprocess with python
我的代码在终端中有两个可能的结果:
1 2 3 4 | from subprocess import check_output out = check_output(["sudo","rfcomm","connect","0","AA:BB:CC:DD:EE:FF","10"]) print"output: %s" % out |
相反,我一无所获:
1 2 3 | user:~/home $./foo.py Can't connect RFCOMM socket: Permission denied output: |
另一种尝试:
1 2 3 | proc = subprocess.Popen(["sudo rfcom connect 0 AA:BB:CC:DD:EE:FF 10"], stdout=subprocess.PIPE, shell=True) (out, err) = proc.communicate() print"output: %s" % out, err |
这至少在我打印时给了我一些东西。 不幸的是,它是"无"告诉我没有错误而不是实际输出:
1 2 3 | user:~/home $./foo.py Can't connect RFCOMM socket: Permission denied output: None |
我已经尝试过这个,也可能是其他几个。 我确定我在某处遗漏了一些关键知识。 感谢您的任何指示!
1 2 | subprocess.check_output(["sudo","rfcomm","connect","0","AA:BB:CC:DD:EE:FF","10"], stderr=subprocess.STDOUT) |