python--调用系统命令

来源:互联网 发布:兄弟连java培训多少钱 编辑:程序博客网 时间:2024/05/29 09:23
使用 os.system() 调用系统命令 , 程序中无法获得到输出和返回值
>>> import os>>> os.system('ls -l /proc/cpuinfo')>>> os.system("ls -l /proc/cpuinfo")  -r--r--r-- 1 root root 0  3月 29 16:53 /proc/cpuinfo  0 
使用 os.popen() 调用系统命令, 程序中可以获得命令输出,但是不能得到执行的返回值
>>> out = os.popen("ls -l /proc/cpuinfo")>>> print out.read()  -r--r--r-- 1 root root 0  3月 29 16:59 /proc/cpuinfo 
使用 commands.getstatusoutput() 调用系统命令, 程序中可以获得命令输出和执行的返回值
>>> import commands>>> commands.getstatusoutput('ls /bin/ls')  (0, '/bin/ls')