Chosing between sys.argv and sys.stdin 
Author Message
 Chosing between sys.argv and sys.stdin

I've got a python script that I want to be used either with commandline
arguments or using stdin (most likely by piping). My question is, what's
the simplest way to code that sort of conditional? My current script
fragment looks like this:

import sys
try:
        text = sys.argv[1]
except IndexError:
        print "usage: ..."
        sys.exit()

How would I add something that says "use sys.stdin if they don't include
commandline arguments, but if neither is present, print the error
message"? Right now, if it calls sys.stdin.read() and it wasn't piped
into (like calling "spam.py" rather than "cat file | spam.py") it just
sits there until I hit ctrl-d.

Note: I don't frequent the newsgroup, so if you could send replies to

----
Syn.Terra
Aevum Industries
http://www.*-*-*.com/



Wed, 16 Apr 2003 06:09:44 GMT  
 Chosing between sys.argv and sys.stdin
Quote:

> I've got a Python script that I want to be used either with commandline
> arguments or using stdin (most likely by piping). My question is, what's
> the simplest way to code that sort of conditional? My current script
> fragment looks like this:

> import sys
> try:
>         text = sys.argv[1]
> except IndexError:
>         print "usage: ..."
>         sys.exit()

Obviously, you will get the IndexError if len(sys.argv) < 2
so this is the key condition to code around.

Quote:
> How would I add something that says "use sys.stdin if they don't include
> commandline arguments, but if neither is present, print the error
> message"? Right now, if it calls sys.stdin.read() and it wasn't piped
> into (like calling "spam.py" rather than "cat file | spam.py") it just
> sits there until I hit ctrl-d.

Probably what you need is something like the following, which
will work for a single file name.  You can easily extend it to
multiple filenames (left as an exercise to the reader...)

if len(sys.argv) < 1:
        myfile = sys.stdin
else:
        try:
                myfile = open(sys.argv[1], 'r')
        except:
                print "Could not open file", sys.argv[1]
                sys.exit(-1)

Quote:
> Note: I don't frequent the newsgroup, so if you could send replies to

> ----
> Syn.Terra
> Aevum Industries
> http://www.aevum.net

Posted and mailed.

regards
 Steve
--
Helping people meet their information needs with training and technology.



Wed, 16 Apr 2003 06:25:32 GMT  
 Chosing between sys.argv and sys.stdin

Quote:
> How would I add something that says "use sys.stdin if they don't include
> commandline arguments, but if neither is present, print the error
> message"? Right now, if it calls sys.stdin.read() and it wasn't piped
> into (like calling "spam.py" rather than "cat file | spam.py") it just
> sits there until I hit ctrl-d.

Look at isatty(), a method of any file object. It returns 1 if the file is connected
to a tty(-like) device, otherwise 0.

Beat Bolli
--
PGP: 0x506A903A; 49D5 794A EA77 F907 764F D89E 304B 93CF 506A 903A
ICBM: 47 02' 43.0" N, 07 16' 17.5" E (WGS84)



Wed, 16 Apr 2003 19:14:26 GMT  
 Chosing between sys.argv and sys.stdin

Quote:

> Probably what you need is something like the following, which
> will work for a single file name.  You can easily extend it to
> multiple filenames (left as an exercise to the reader...)

something like:

myfiles = map(open, sys.argv[1:]) or [sys.stdin]
for file in myfiles:
   input = file.read()

(put inside try-except for error handling)

--
Arne Martin



Thu, 17 Apr 2003 00:00:21 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. using sys.stdin.raw_input and sys.stdin.readline together in a platform independent way

2. understanding sys.argv[]

3. sys module - argv, but no argc ??

4. sys.argv and while loop

5. sys.argv not found

6. catch empty sys.argv

7. sys.argv in Python 2.0 does not work?

8. ?sys.argv values more literally?

9. Win32 problems: Popen3 and sys.argv[0]

10. Sys.argv

11. sys.argv attribute error

12. Specifiying a module from sys.argv?

 

 
Powered by phpBB® Forum Software