LoginSignup
1
0

More than 5 years have passed since last update.

Windows環境のpython(2.x系列)でstdinからstdoutにデータを流し続けるプログラム(バイナリ対応)

Last updated at Posted at 2019-01-31
yokonagashi.py
import sys

if sys.platform == "win32":
    import os, msvcrt
    msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
    msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)

while 1:
    sys.stdout.write(sys.stdin.read())
  • 実行
    • cat hoge.bin | python -u yokonagashi.py > hoge_dup.bin
    • -u オプションはなんかstdin, stdout, stderr あたりでバイナリを使うときに必要らしい。でもこのコードだといらないかも。未確認。
    • yokonagashi.py はEOFとか見てないので、上のコマンドラインは実行すると終了しない。Ctrl-cとかで止めて下さい。多分、出力はちゃんとされているはず。

なんかこれだけのコードを書くために、結構ググった。
cat とか使ってるのは MSYS2環境 だから。

1
0
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
1
0