Python不会读取sys.argv [英] Python will not read sys.argv

查看:174
本文介绍了Python不会读取sys.argv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    import sys

    print sys.argv[1]

这似乎很基本,但是我无法让Python从命令行读取任何内容。
就是上面的代码,我输入的是:

this may seem very basic but I can't get Python to read in anything from the command line. thats the code above and what I type is:

    myfile.py helloworld

我得到的是:

    IndexError: list index out of range

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

It seemed to work once for me but won't work any more, and I've tried uninstalling and reinstalling Python and it still doesnt work.

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

So my question is, am I doing anything wrong? or have I just broken Python?

感谢您的帮助

使用:
Windows 7
Python 2.7.2

Using: Windows 7 Python 2.7.2

推荐答案

您确定要以自己的方式调用python脚本吗?

Are you sure you are calling your python script the way you think you are?

#!/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
$ 

这篇关于Python不会读取sys.argv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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