gpt4 book ai didi

python - 使用python返回参数到命令行

转载 作者:行者123 更新时间:2023-12-05 08:35:07 24 4
gpt4 key购买 nike

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

我想用 python 获取参数“abc”,然后让批处理文件使用参数“abc”做其他事情。

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

感谢您的帮助。

最佳答案

你没有说你正在使用什么环境(*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.

编辑(适用于 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 的方式漂亮和整洁。

关于python - 使用python返回参数到命令行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10847784/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com