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

标签 python command-line

我想在批处理文件和 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/

相关文章:

python - 不损失使用cv2.imwrite()保存的图片的质量

python - Django Google 应用引擎引用问题

linux - 如何在 Linux 中更改 echo 的输出颜色

command-line - 用于在非默认端口上连接 extssh 的 CVS 命令

java - 使用不同的工作目录从命令行运行 java 应用程序

python - Flask-Marshmallow 模式上的验证错误

python - Python 中的释放内存

python - 我用Python编写了一段代码,即使我使用break语句并继续,它仍然会进入Python :的无限循环

Linux 30 位随机数生成器

ios - 如何为 React Native 构建 .ipa?