您的位置:首页 > 其它

subprocess使用

2014-02-21 17:28 162 查看
1. Popen使用

test = subprocess.Popen('ls /tmpa', shell=True, stdout = subprocess.PIPE, stderr=subprocess.PIPE)

print test.stdout.read()

print test.stderr.read()

说明: shell=True代表 unix下相当于args前面添加了 "/bin/sh“ ”-c”, window下,相当于添加"cmd.exe /c",

stdout、stderr输出到PIPE中否则会直接print出来,我们使用Popen就是为了获取这些结果,当然放到PIPE管道中了

PIPE管道是类文件对象,可调用read方法来获得

2.call使用

test = subprocess.call('ls /tmp', shell=True)

print test

说明:call是执行命令,返回的值是执行状态的结果,成功是0.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: