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

我想在批处理文件和 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 的方式漂亮和整洁。

https://stackoverflow.com/questions/10847784/

相关文章:

django - 从 Django 中的一系列 id 中检索匹配对象的列表

php - 将时间戳转换为 .ics iCal 兼容的日期格式

ruby-on-rails - capistrano 错误 : . ..../current: 没有

python - 如何使用 python grep 大文件中两个模式之间的行

jruby - bundler 无法识别平台?找不到 gem

django - 重定向到管理员登录

wordpress - 在单个页面上显示 WordPress 博客的所有评论?

ruby-on-rails - 事件管理员 : pass instance variable for

php - 使用 PHP DOM 方法读取 iTunes XML 文件

c# - C#中如何从子构造函数调用父构造函数