[英]Python subprocess.popen returns empty string
import subprocess
cd=['sudo','./interface','-a','</tmp/vol.js']
p = subprocess.Popen(cd, stdout = subprocess.PIPE,stderr=subprocess.PIPE, stdin=subprocess.PIPE)
Above code returns null but when I run same same command ie sudo ./interface -a </tmp/vol.js
works totally fine 上面的代码返回null,但是当我运行相同的命令时,即
sudo ./interface -a </tmp/vol.js
可以正常工作
You are using shell-specific redirection syntax ( </tmp/vol.js
) but subprocess.Popen()
doesn't run your command in a shell. 您正在使用特定于Shell的重定向语法 (
</tmp/vol.js
),但是</tmp/vol.js
subprocess.Popen()
不会在Shell中运行命令。
You'd need to either run the command in a shell (pass in a string , not a list, and set shell=True
), or open vol.js
yourself and write that to the process through the stdin
pipe you opened: 您需要在外壳中运行命令(传递一个字符串 ,而不是一个列表,并设置
shell=True
),或者自己打开vol.js
并将其通过打开的stdin
管道写入进程:
import subprocess
command = ('sudo', './interface', '-a')
with open('/tmp/vol.js') as input_stream:
p = subprocess.Popen(command, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stdin=input_stream)
stdout, stderr = p.communicate()
Note that I passed in the open vol.js
file object as the input stream here. 请注意,我在此处传递了打开的
vol.js
文件对象作为输入流。
Can you try this. 你可以试试这个吗
import subprocess
cd = ['sudo','./interface','-a','</tmp/vol.js']
cd_cmd = " ".join(cd)
o, e = subprocess.Popen(cd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
The output is stored in variable "o" and stderr in "e" 输出存储在变量“ o”中,stderr存储在“ e”中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.