1

バッチファイルとpythonプログラムの間の接続を構築したい。

Pythonを使用して「abc」というパラメーターを取得し、バッチファイルでパラメーター「abc」を使用して他のことを行いたいと考えています。

Python でコマンドラインにパラメータを返すにはどうすればよいですか?

ご協力いただきありがとうございます。

4

2 に答える 2

10

使用している環境 (* nix/Windows/OSX など) は言いませんが、* nix およびシェルスクリプトの場合は実行できます

# Python
# whatever.py
import sys
sys.stdout.write('abc')
sys.exit(0)

# In your shell
OUT=`python whatever.py`
echo $OUT
# Will print abc, and it's stored in the variable `OUT` for later consumption.

編集 (Windows の場合):

# Python
# whatever.py
import sys
sys.stdout.write('abc')
sys.exit(0)

# In a .bat file, or cli.
python whatever.py > temp.txt
set /p OUT=<temp.txt
# Creates/replaces a file called temp.txt containing the output of whatever.py
# then sets the `OUT` var with the contents of it.

残念ながら、Windows の方法は、*nix の方法ほど適切ではありません。

于 2012-06-01T09:39:50.510 に答える