[Tutor] Command line args

Luke Paireepinart rabidpoobear at gmail.com
Mon Apr 9 23:42:49 CEST 2007


Kirk Bailey wrote:
> Teresa Stanton wrote:
>   
>> If one argument to a script is provided I am to take the input from it.  
>> I figure that is presented like this:
>>
>> filename = sys.argv[1]
>>     
> Try:
>   
the 'try' keyword is not capitalized in Python.
> 	filename=sys.arg[1]
> except exception, E:
>   
you should only catch the exception you expect, so you don't 
accidentally silence an unrelated error.
so
except IndexError:
because you're trying to index into a list that might not have 2 or more 
elements.
> 	filename='FooBar'
>   
You said you wanted the input from standard input, so just put a 
raw_input here.

so the new code is:

try: filename = sys.argv[1]
except IndexError: filename = raw_input("Prompt: ")

HTH,
-Luke


More information about the Tutor mailing list