使用python返回参数命令行

Jim*_*Lin 0 python command-line

我想在批处理文件和python程序之间建立连接.

我想使用python来获取一个参数"abc",并让批处理文件使用参数"abc"来做其他事情.

如何在python中将参数返回到命令行?

谢谢你的帮助.

Chr*_*tts 7

你没有说你正在使用什么环境(*nix/Windows/OSX等),但对于*nix和shell脚本,你可以做

# 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.
Run Code Online (Sandbox Code Playgroud)

编辑(适用于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.
Run Code Online (Sandbox Code Playgroud)

不幸的是,Windows的做法并不像*nix那样漂亮和整洁.