繁体   English   中英

遍历sys.args

[英]Loop over sys.args

当尝试循环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

这就是我调用程序的方式:

./autoCrosRef.py file.txt

这是我用来尝试遍历的代码:

import sys
# ------
#  MAIN
# ------
inputArgs = sys.argv

print len(inputArgs)

for i in inputArgs[1:]:

    cleanFile(inputArgs[i])

我的打印命令显示我在cmd处传递了2个args,但它始终出错,是我称错还是循环错了?

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 inputArgs[1:]:
    # i is an item from inputArgs. If inputArgs=['foo', 'bar', 'baz']
    # then i is first 'bar' then 'baz'.
    cleanFile(i)

暂无
暂无

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

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