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'>
标签:obj,err,python,res,subprocess,模块,print,out 来源: https://www.cnblogs.com/weiweirui/p/16379217.html