passing file from command line startup

Robert Kern robert.kern at gmail.com
Wed Aug 11 15:03:11 EDT 2010


On 8/11/10 1:47 PM, Bradley Hintze wrote:
> Hi all,
>
> Is there a way that I can startup my script and pass it a file? For example:
>
> ~$ python myscript.py mytext.txt
>
> and then access mytext.txt in myscript.py?
>
> As a long shot, for myscript.py I tried
>
> def __init__(fle):
>      print fle
>
> expecting the full path to mytext.txt to be printed but that didn't work.
>
> Obviously I've never done this. I hope the above makes sense. any help
> will be greatly appreciated.

http://docs.python.org/library/sys#sys.argv

I do recommend using argparse to process command line arguments, even for the 
simplest cases:

   http://docs.python.org/library/argparse

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list