python - 如何在 Windows 命令行中使用参数运行 Python 脚本

标签 python windows python-2.7

这是我的 python hello.py 脚本:

def hello(a,b):
    print "hello and that's your sum:"
    sum=a+b
    print sum
    import sys

if __name__ == "__main__":
    hello(sys.argv[2])

问题是不能从windows命令行提示符运行,我用的是这个命令:

C:\Python27>hello 1 1

不幸的是,它没有用,有人可以帮忙吗?

最佳答案

  • import sys退出 hello 函数。
  • 参数应转换为 int。
  • 包含 ' 的字符串字面量应该转义或应该被 " 包围.
  • 您是否使用 python hello.py <some-number> <some-number> 调用了该程序?在命令行中?

import sys

def hello(a,b):
    print "hello and that's your sum:", a + b

if __name__ == "__main__":
    a = int(sys.argv[1])
    b = int(sys.argv[2])
    hello(a, b)

关于python - 如何在 Windows 命令行中使用参数运行 Python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17544307/

相关文章:

python - scipy stats kstest 针对 x=y?

windows - 我在哪里可以找到 Windows 中的 vimrc 文件?

windows - ln -s <target folder> <link folder> unix symbolic link命令在windows中的作用是什么?

python - 访问剪贴板 Windows (7) 64 位

python - 对常量进行分类(并能够确定其类别)

json - 使用postgresql从Flask-SQLAlchemy中的JSON字段查询值

python - 如何使用 matplotlib 更新图形

python - 如何在 Windows 上使用 Python 3 连接到 MySQL?

Python:遍历嵌套列表列表中的每个项目并替换特定项目

python - 如何使用Python编程语言发送带有客户端证书的https请求