AttributeError:'module'对象在访问命令行参数python时没有属性'arvg'

Shi*_*ade 0 python attributes runtime-error argv command-line-arguments

我访问命令行参数的代码:

length = len(sys.argv)
print length
str = sys.arvg[0]
print str
Run Code Online (Sandbox Code Playgroud)

给出输出:

triongle@triongle.com [~/download/DataInsertionScript]# python IngestDataToMongo.py python_sucks
2
Traceback (most recent call last):
File "IngestDataToMongo.py", line 83, in <module>
str = sys.arvg[0]
AttributeError: 'module' object has no attribute 'arvg'
Run Code Online (Sandbox Code Playgroud)

所以,第一次len(sys.argv)工作,但奇怪的是下次在sys.argv [0]上给出错误.那为什么呢?

Nli*_*tis 6

你写的sys.arvg[0]而不是sys.argv[0].此外,一定要importsys模块,因为argv的默认属于该命名空间.