0

スクリプトを作成しようとしていますが、使用しているユーティリティの1つがstdoutに値を返し、変数に割り当てたいと考えています。

ユーティリティ(candump)は常に実行されており、データを受信したときにのみstdに出力されます。

import threading
from subprocess import call
import time

class ThreadClass(threading.Thread):
  def run(self):
    call(["candump","can0"])

t = ThreadClass()
t.start()
time.sleep(1)
call(["cansend", "can0", "-i", "0x601", "0x40", "0xF6", "0x60", "0x01", "0x00", "0x00", "0x00", "0x00"])

これは、Pythonスクリプトで使用したい次の値を返します。

<0x601> [8] 40 f6 60 01 00 00 00 00
<0x581> [8] 4b f6 60 01 96 08 00 00

candump(stdoutにデータをダンプするもの)に関するドキュメントはまばらです

stdoutを利用して、そこに送られるデータを取得する方法はありますか?

これが信じられないほど明白な場合は申し訳ありません...Linuxを少しずつ学んでいます。

4

1 に答える 1

2

大量の出力を期待していない場合、または一度にすべてを読んでもかまわない場合は、次を使用できますsubprocess.check_output

>>> import subprocess
>>> print subprocess.check_output(['ls', '/etc'])
adjtime
adobe
anacrontab
apparmor.d
arch-release
ati
at-spi2
avahi
axelrc
bash.bash_logout

If you do need to read it line-by-line, take a look at this question: read subprocess stdout line by line

于 2013-03-04T23:28:53.337 に答える