Re: piping into a python script



On Jan 24, 3:25 pm, Marc 'BlackJack' Rintsch <bj_...@xxxxxxx> wrote:
On Thu, 24 Jan 2008 17:17:25 +0200, Donn Ingle wrote:
Given these two examples:
1.
./fui.py *.py
2.
ls *.py | ./fui.py

How can I capture a list of the arguments?
I need to get all the strings (file or dir names) passed via the normal
command line and any that may come from a pipe.

There is a third case:
3.
ls *.jpg | ./fui.py *.png
Where I would be gathering strings from two places.

I am trying to write a command-line friendly tool that can be used in
traditional gnu/linux ways, otherwise I'd skip the pipe stuff totally.

I have tried:
1. pipedIn = sys.stdin.readlines()
Works fine for example 2, but example 1 goes into a 'wait for input' mode
and that's no good. Is there a way to tell when no input is coming from a
pipe at all?

Usually Linux tools that can get the data from command line or files treat
a single - as file name special with the meaning of: read from stdin.

So the interface if `fui.py` would be:

1. ./fui.py *.a
2. ls *.a | ./fui.py -
3. ls *.a | ./fui.py *.b -

Ciao,
Marc 'BlackJack' Rintsch

If X.a X.b Y.a Y.b are all files whose contents are to be processed
then
To process all files:
./fui.py *.a *.b
Or:
./fui.py `ls *.a *.b`
To process one file from a pipe unix usually does:
cat X.a | ./fui.py -
To get the filenames from stdin would usually need a command line
switch telling fui.py to read a file *list* from stdin. For verilog
simulators for example you have the -f switch that says insert further
command line arguments from the file name in the next argument, so you
could do:
ls *.a | ./fui.py -f - *.b
For equivalent functionality to my first example.

- Paddy.


.



Relevant Pages

  • Re: piping into a python script
    ... Where I would be gathering strings from two places. ... otherwise I'd skip the pipe stuff totally. ... Usually Linux tools that can get the data from command line or files treat ... Did anyone mention the fileinput module? ...
    (comp.lang.python)
  • Re: How to check if a thread waits for data at the read-end of a pipe?
    ... If thread in waiting mode you can use GetThreadContext ... > attach a semaphore to a named pipe and I can then use the other API ... > My application acts as a GUI command line shell in this regard. ... > child's STDIN, STDOUT and STDERR streams in standard documented ways. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: How to redirect stdout to stdin?
    ... stdout of nc to its stdin. ... If you could connect the output of a command to its input, ... Use a pair of pipes, at least one of which must be a named pipe ...
    (comp.unix.shell)
  • Re: piping into a python script
    ... Where I would be gathering strings from two places. ... otherwise I'd skip the pipe stuff totally. ... Usually Linux tools that can get the data from command line or files treat ...
    (comp.lang.python)
  • How to check if a thread waits for data at the read-end of a pipe?
    ... Win32 and in the Win32 version I need a way to check ... attach a semaphore to a named pipe and I can then use the other API ... My application acts as a GUI command line shell in this regard. ... child's STDIN, STDOUT and STDERR streams in standard documented ways. ...
    (microsoft.public.win32.programmer.kernel)