[Tutor] sending both a filename and an argument from the command line

David Rock david at graniteweb.com
Sat Nov 22 21:37:25 EST 2003


* Ben Vinger <bvinger at postmaster.co.uk> [2003-11-23 00:10]:
> Hi
> It seems I can send either a file(s), or arguments to a python script,
> but not both:
> I want to do:
>
> python ip.py myfile -i myargument
> or even:
> cat myfile | python ip.py -i myargument
> 
> Neither works, although if I do
> 
> cat myfile | python ip.py - myargument
> 
> the program begins working and even prints out some correct output
> before aborting with:
> 
> IOError: [Errno 2] No such file or directory: 'myargument'
> 
> Is there a way to accomplish this?

Most modules that parse commandlines (like getopt) expect that the
arguments come first, then the filenames. So 
   python ip.py myfile -i myargument
should be
   python ip.py -i myargument myfile 

piping data into a python app needs some special handling so that it can
understand what you are trying to give it. 

  cat myfile | python ip.py - myargument

This probably works because it thinks "-" is a filename that represents
stdin. Without seeing your code, I can't be sure.

The way I usually handle this kind of argument/filename processing is a
combination of the getopt and fileinput modules.

http://www.python.org/doc/current/lib/module-getopt.html
http://www.python.org/doc/current/lib/module-fileinput.html

-- 
David Rock
david at graniteweb.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://mail.python.org/pipermail/tutor/attachments/20031122/486f482f/attachment.bin


More information about the Tutor mailing list