返回顶部

python之subprocess模块

python之subprocess模块

  • subprocess模块执行系统命令【子进程执行】
  • os.system('dir')   调用shell命令
#显示正确结果
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'>

 

posted @ 2022-06-15 17:28  九尾cat  阅读(142)  评论(0编辑  收藏  举报