Chinaunix

标题: python中获取shell命令的返回结果? [打印本页]

作者: zing2046    时间: 2011-10-17 22:09
标题: python中获取shell命令的返回结果?
python在执行shell命令使用os.system,如:os.system('command'),那我如何把command的执行结果存放在一个变量里面呢?os.system返回的仅仅是一个退出码。
作者: dacheng    时间: 2011-10-17 22:57
可以使用subprocess,例:
import subprocess

p=subprocess.Popen('ls',stdout=subprocess.PIPE)
a=p.stdout.read().decode('utf8')
print(a)
作者: 南国书生    时间: 2011-10-18 10:07
很基础的问题, 用os.popen()就可以
作者: dahe_1984    时间: 2018-02-06 16:19
shell的返回值多行多列怎么处理好呢?




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2