On Fri, Jun 10, 2011 at 11:58 AM, Mark Phillips <m...@phillipsmarketing.biz> wrote: \ > > Kurt, > > How does one write a main method to handle both command line args and stdin > args?
Here is what I came up with: The one weird thing, the line from above didn't seem to work so I changed it if os.isatty(sys.stdin): to this: if not os.isatty(sys.stdin.fileno()): Below 3 tests, with stdin redirection, then with one argument, then with both stdin redirection and one argument, the results are at the very bottom. $ cat ./argvtest.py; echo "fred" | ./argvtest.py ; ./argvtest.py "alice"; echo "fred" | ./argvtest.py "bob" #!/usr/bin/python import os import sys def main(): #This checks to see if stdin is a not tty and > 0 if not os.isatty(sys.stdin.fileno()): arg = sys.stdin.read() print arg elif len(sys.argv[1:]) > 0: # if the length of the first argument is > 0 #[1:] strip the first string beacause it is the name of the script arg = sys.argv[1:] print arg if __name__ == "__main__": main() fred ['alice'] fred > > Thanks, > > Mark > Welcome, Dennis O. > -- > http://mail.python.org/mailman/listinfo/python-list > > -- http://mail.python.org/mailman/listinfo/python-list