编程语言
首页 > 编程语言> > python之subprocess模块

python之subprocess模块

作者:互联网

python之subprocess模块

#显示正确结果
import subprocess
obj=subprocess.Popen('ls /root',shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
print(obj)
out_res=obj.stdout.read()
err_res=obj.stderr.read()
print(out_res.decode('utf-8'),type(out_res))
print(err_res.decode('utf-8'),type(err_res))

>>> import subprocess
>>> obj=subprocess.Popen('ls /root',shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
>>> print(obj)
<subprocess.Popen object at 0x7fa65ea0a6d8>
>>> out_res=obj.stdout.read()
>>> err_res=obj.stderr.read()
>>> print(out_res.decode('utf-8'),type(out_res))
nginx-centos_config.tar.gz
nginx-centos_dockerfile.tar.gz
nginx-centos_image.tar.gz
SSL
weirui
zhaopian
 <class 'bytes'>
>>> print(err_res.decode('utf-8'),type(err_res))
 <class 'bytes'>

 

标签:obj,err,python,res,subprocess,模块,print,out
来源: https://www.cnblogs.com/weiweirui/p/16379217.html