LoginSignup
4
4

More than 3 years have passed since last update.

powershell で python の出力を受け取る

Posted at
hello.py
print("こんにちは!")
print("hello!")

上記の .py ファイルがあるとき、powershellでは下記のように実行して print 文の内容をコンソールに出力させることができます。そして powershell は python の標準出力を戻り値として受け取ることができます。

PS > python .\hello.py
こんにちは!
hello!

複数回の print 文の内容は配列に格納され、いったん変数に格納してしまえば通常のオブジェクトとして処理することができます。

PS > $s = (python .\hello.py)
PS > $s
こんにちは!
hello!

PS > $s[0]
こんにちは!

PS > $s[0] -replace "!", "?"
こんにちは?

コマンドライン引数も与えられるので .py ファイルが1つの関数のように機能します。python の柔軟な文字列操作を利用したいときに使えそうな tips として紹介してみました。

4
4
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
4
4