且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用python运行带有Popen的shell脚本在python命令行和实际程序中的行为有所不同

更新时间:2023-12-04 22:24:40

之所以没有从命令中获得任何输出,是因为您没有告诉子进程打开用于​​通信的管道...

The reason you are not getting any output from the command is because you have not told the subprocess to open pipes for communication...

from subprocess import Popen, PIPE

sh = Popen("sudo %s" % bf_path, shell=True, stdout=PIPE, stderr=PIPE)

现在,通讯将返回stdout和stderr的输出.另外,如果需要输入密码,sudo可能会给您带来麻烦.

Now communicate will return the output of both stdout and stderr. Also, its possible that the sudo might cause you problems if it requires a password input.