6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

pexpect使い方メモ

Posted at

対話型プログラムのラッパーを作るときに使ったpexpectのメモ

import pexpect
# 任意のコマンドを実行する子プロセスのインスタンスを生成
p = pexpect.spawn("some command")

# 任意の入力を行う
p.send("yes")
# 任意の入力+改行を行う
p.sendline("yes")

# 指定した正規表現が表示されるまで待機する
p.expect(r"> ")

# expectがマッチしたときのインスタンス変数
data = {
    "正規表現にマッチした文字列より前に標準出力されていた文字列全て": p.before,
    "正規表現にマッチした文字列": p.after,
    "正規表現マッチ時、マッチした文字列より後に標準出力されていた文字列全て": p.buffer
}
6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?