如何在 Windows 命令行中使用参数运行 Python 脚本 [英] How do I run Python script using arguments in windows command line

查看:22
本文介绍了如何在 Windows 命令行中使用参数运行 Python 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 python hello.py 脚本:

This is my python hello.py script:

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命令行提示符下无法运行,我用了这个命令:

The problem is that it can't be run from the windows command line prompt, I used this command:

C:\Python27>hello 1 1

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

But it didn't work unfortunately, may somebody please help?

推荐答案

  • import sys 出 hello 函数.
  • 参数应转换为 int.
  • 包含 ' 的字符串文字应该被转义或者应该被 " 包围.
  • 您是否使用 python hello.py <some-number> 调用了该程序? 在命令行中?
    • import sys out of hello function.
    • arguments should be converted to int.
    • String literal that contain ' should be escaped or should be surrouned by ".
    • Did you invoke the program with python hello.py <some-number> <some-number> in command line?
    • 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)
      

      这篇关于如何在 Windows 命令行中使用参数运行 Python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆