我有使用perl编写脚本的经验,这使我通过使用back ticks执行linux命令变得很容易我在想,我该怎么做这条蟒蛇?有没有一种特殊的方法来捕获命令(输出)的结果?
谢谢:)
最佳答案
要添加到urschrei的答案中,以下是一个示例(windows):
>>> import subprocess
>>> p = subprocess.Popen(['ping', '192.168.111.198'], stdout=subprocess.PIPE, st
derr=subprocess.PIPE)
>>> out, err = p.communicate()
>>> print out
Pinging 192.168.111.198 with 32 bytes of data:
Reply from 192.168.111.198: bytes=32 time<1ms TTL=128
Reply from 192.168.111.198: bytes=32 time<1ms TTL=128
Reply from 192.168.111.198: bytes=32 time<1ms TTL=128
Reply from 192.168.111.198: bytes=32 time<1ms TTL=128
Ping statistics for 192.168.111.198:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms
>>> print err
>>> print p.returncode
0