python执行系统commandline

By | 2月 5, 2016
命令名称 操作系统 特点
os.system Window & linux 返回0正确,1错误
os.open Window & linux 输出以文件返回
commands.getoutput Linux 输出以字符串返回

Examples

import os
import commands

#(Support Windows & Linux) Popup DOS dialog in windows.
os.system("echo a>C:\\Localdata\\123456.txt")

#(Support Windows & Linux) No dialog popups
output = os.popen("dir C:\\LocalData")
print output.read()

for line in os.popen("dir C:\\"):
    print line

#(Only support Linux)
output = commands.getoutput("pwd")