[英]How subprocess.popen is used
Why do I get an error when I run subprocess.popen?为什么我在运行 subprocess.popen 时会出错?
I load a command with arguments in a variable, I do a split and pass the result to subprocess.Popen.我在变量中加载带有 arguments 的命令,进行拆分并将结果传递给 subprocess.Popen。 It returns an error .
它返回一个错误。
Thank you all very much for your collaboration非常感谢大家的合作
The complete code is as follows:完整代码如下:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import subprocess
import shlex
def main():
try:
data = "ls -al"
args = shlex.split(data)
comando = subprocess.Popen(args, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
if comando.stderr.read() != "":
print("[-] Error")
else:
print(comando.stdout.read())
except Exception as e:
print (e)
if __name__ == '__main__' :
try:
main()
except KeyboardInterrupt:
exit()
Use communicate使用交流
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import subprocess
import shlex
def main():
try:
data = "ls -al"
args = shlex.split(data)
comando = subprocess.Popen(args, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
comando_stdout, comando_stderr = comando.communicate()
if comando_stderr:
print("[-] Error")
else:
print(comando_stdout)
except Exception as e:
print (e)
if __name__ == '__main__' :
try:
main()
except KeyboardInterrupt:
exit()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.