I wrote this code:
#!/usr/bin/env python
import sys
if sys.argv[1] :
print sys.argv[1]
Try this in console when typed: $ python py.py xxx that prints xxx When i leave it with no parameter an error appears:
Traceback (most recent call last): File "py.py", line 4, in if sys.argv[1] : IndexError: list index out of range
Now with a few changes:
#!/usr/bin/env python
import sys
if sys.argv[1:] :
print sys.argv[1:]
You see i changed [1] to [1:] as well and now if i type "$ python py.py " in console and forget the parameter that returns no error. Whats happen in the behind the scene?