When trying to loop over sys.args I'm getting the following error:
Traceback (most recent call last):
File "./autoCrosRef.py", line 59, in <module>
cleanFile(inputArgs[i])
TypeError: list indices must be integers, not str
This is how I am calling my program:
./autoCrosRef.py file.txt
and this is the code I'm using to try and loop over it:
import sys
# ------
# MAIN
# ------
inputArgs = sys.argv
print len(inputArgs)
for i in inputArgs[1:]:
cleanFile(inputArgs[i])
my print command shows that I am passing 2 args at cmd but it keeps erroring, am I calling it wrong or is my loop wrong?
i there is an item in your inputArgs array - not an index. You could either do
for i in range(1, len(inputArgs)):
# i is a number, from 1 to len(inputArgs)-1
cleanFile(inputArgs[i])
or (this one is preferred)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With