Python中执行外部命令并捕获双向输出

来源:互联网 发布:php 随机数原理 编辑:程序博客网 时间:2024/05/18 04:52
import subprocess# print ’popen3:’def external_cmd(cmd, msg_in=''):       try:               proc = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE,)                stdout_value, stderr_value = proc.communicate(msg_in)         return stdout_value, stderr_value        except ValueError, err:         # log("IOError: %s" % err)               return None, Noneif __name__ == '__main__':        stdout_val, stderr_val = external_cmd('ls -l')        print 'Standard Output: %s' % stdout_val        print 'Standard Error: %s' % stderr_val
0 0
原创粉丝点击