How can it be that len(sys.argv) <= 0?

856 views Asked by At

In my code, the following error triggered, and I do not understand how this case can come up:

if(len(sys.argv) > 0):
    doSomething()
else:
    raise AttributeError("Could not parse script name")

The above code is in a python class, which I import and use in some script. I use the same class with the same call in other scripts, and it works just fine everywhere else. FYI, my OS is ubuntu.

How is it even possible that len(sys.argv) is <= 0?

1

There are 1 answers

0
Droids On BEST ANSWER

Ok, so we found the answer; @nneonneo gave the right hint, at some point actually argv was modified:

args = sys.argv
del args[0]

I guess the author of that code wanted to do something different, because this actually also deletes sys.argv[0]. We are looking to change it in the following way:

args = sys.argv[1:]

Thank you!