3

スクリプトを書くために perl を使用した経験があるため、バックティックを使用して Linux コマンドを簡単に実行できました。どうすればこの Python を実行できますか? コマンドの結果 (出力) をキャプチャする特別な方法はありますか?

ありがとうございました :)

4

2 に答える 2

6

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
于 2011-08-19T22:21:52.380 に答える
2

subprocessモジュール、具体的にはsubprocess.check_call()および/またはsubprocess.check_output()コマンドを探しています。

于 2011-08-19T22:00:57.993 に答える