执行系统命令中比较经常使用的就是os.system()和os.popen()两种方法code
那么有什么区别呢?
首先都须要导入os包:import
import os
os.system()方法能够直接使用,会直接执行系统命令并输出结果,可是没有返回值
好比说:变量
os.system('ls') connect.py helloworld.py result = os.system('ls') connect.py helloworld.py result 0
成功执行系统命令,可是返回值直接为0file
os.popen()方法也会执行命令,而且须要将结果保存到变量中,可是最好不要直接使用方法
若是直接使用,就会像这样:im
os.popen('ls') <open file 'ls', mode 'r' at 0x107df5c00>
因此通常都是这样使用:co
result = os.popen('ls').readline() result ['connect.py\n', 'helloworld.py\n']