繁体   English   中英

Python不会读取sys.argv

[英]Python will not read sys.argv

    import sys

    print sys.argv[1]

这可能看起来非常基本,但我不能让Python从命令行读取任何内容。 这就是上面的代码和我输入的内容:

    myfile.py helloworld

我得到的回报是:

    IndexError: list index out of range

它似乎对我有用,但不再适用,我尝试卸载并重新安装Python,但它仍然无法正常工作。

所以我的问题是,我做错了什么? 还是我刚刚打破了Python?

谢谢你的帮助

使用:Windows 7 Python 2.7.2

启动注册表编辑器(regedit)。 HKEY_CLASSES_ROOT \\ Applications \\ python26.exe \\ shell \\ open \\ command键设置为: "C:\\Python26\\python26.exe" "%1" %*

此解决方案的来源: http//eli.thegreenplace.net/2010/12/14/problem-passing-arguments-to-python-scripts-on-windows/

你确定你以你认为的方式调用你的python脚本吗?

#!/usr/bin/python
import sys

if len(sys.argv) < 2:
    print "you did not give any arguments\n"
else:
    print sys.argv[1]

收益:

$ ./foo.py 
you did not give any arguments

$ ./foo.py hello_world
hello_world
$ 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM