# -*- coding:UTF-8 -*-
import sys,os
import subprocess
valuea="bob"
valueb="say some thing!!"
command="echo 'hello world'"
#第一种:os.system()
os.system(command) #返回值为0则成功
#第二种:os.popen()
with os.popen(command,'r') as a: #返回值是文件对象
print(a.read())
a=os.popen(command,'r')
print(a.read())
#第三种:subprocess.check_output()
data=subprocess.check_output(command,shell=True)
print( data)
command="echo 'hello %s,please %s'" % (valuea,valueb)
os.system(command) #返回值为0则成功
with os.popen(command,'r') as a: #返回值是文件对象
print(a.read())
a=os.popen(command,'r')
print(a.read())
data=subprocess.check_output(command,shell=True)
print( data)
python程序调用shell指令的多种方法
最新推荐文章于 2023-03-11 19:50:04 发布