返回顶部

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 @   九尾cat  阅读(144)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
历史上的今天:
2021-06-15 vsftpd启动报错:vsftpd:500 OOPS: bad bool value in config file for: anonymous_enable
2020-06-15 管理创建分区命令
点击右上角即可分享
微信分享提示