Python 在sys.args上循环
当尝试在Python 在sys.args上循环,python,linux,for-loop,command-line-arguments,Python,Linux,For Loop,Command Line Arguments,当尝试在sys.args上循环时,出现以下错误: Traceback (most recent call last): File "./autoCrosRef.py", line 59, in <module> cleanFile(inputArgs[i]) TypeError: list indices must be integers, not str 我的print命令显示我在cmd上传递了2个参数,但它一直出错,是我调用错误还是我的循环错误?I您的inputAr
sys.args
上循环时,出现以下错误:
Traceback (most recent call last):
File "./autoCrosRef.py", line 59, in <module>
cleanFile(inputArgs[i])
TypeError: list indices must be integers, not str
我的print命令显示我在cmd上传递了2个参数,但它一直出错,是我调用错误还是我的循环错误?
I
您的inputArgs数组中有一个项,而不是索引。你也可以这样做
for i in range(1, len(inputArgs)):
# i is a number, from 1 to len(inputArgs)-1
cleanFile(inputArgs[i])
或者(最好是这个)
for i in range(1, len(inputArgs)):
# i is a number, from 1 to len(inputArgs)-1
cleanFile(inputArgs[i])
for i in inputArgs[1:]:
# i is an item from inputArgs. If inputArgs=['foo', 'bar', 'baz']
# then i is first 'bar' then 'baz'.
cleanFile(i)