subprocess使用

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.

        

原文地址:https://www.cnblogs.com/cmsd/p/3559882.html