1.不需要获取命令执行结果
os.system(command)
# -*- coding:utf-8 -*- import os command='adb devices' tt=os.system(command) print(tt) print(type(tt))
输出结果为:
0
2.需要获取命令执行结果
tt=os.popen(command)
# -*- coding:utf-8 -*- import os command='adb devices' tt=os.popen(command) print(tt) print(type(tt)) tmp=tt.read() print(tmp)
输出结果为:
List of devices attached CVH7N16A12000234 device
总结:
python调用Shell脚本或者是调用系统命令,有两种方法:
1.os.system(cmd)
2.os.popen(cmd)
前者返回值是命令执行后的退出状态码,正常为0,异常为1
后者的返回值是脚本执行过程中的输出内容。
3.subprocess获取执行结果
st = subprocess.Popen('pwd', stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True).stdout.readline()
print(bytes.decode(st))
# /usr1



