python可以非常容易的捕获程序的输出信息,同时也可以传递给命令行程序信息
#捕获程序输出信息 #!/usr/bin/python import subprocess myprocess = subprocess.Popen(['net','statistics','workstation'],stdout=subprocess.PIPE) (sout,serr) = myprocess.communicate() for line in sout.split('\n'): if line.strip().startswith('Bytes received'): print "This workstation received %s bytes." % line.strip().split(' ')[-1] #传递给命令行程序信息 #!/usr/bin/python import subprocess myprocess = subprocess.Popen(['net','statistics','workstation'],stdout=subprocess.PIPE) (sout,serr) = myprocess.communicate() for line in sout.split('\n'): if line.strip().startswith('Bytes received'): print "This workstation received %s bytes." % line.strip().split(' ')[-1] myprocess.wait() # We wait for process to finish print myprocess.returncode # then we get its returncode.