Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #7400
| References | <BANLkTi==QHd+5C=xf+2Re1Mh4V6Hk972hg@mail.gmail.com> <4DF25738.2020704@mrabarnett.plus.com> <BANLkTimHfZK=FsbSs4BOX8dU=mt2bVGV9A@mail.gmail.com> <BANLkTikOzwCNMtzP2gDtbhZF-cRDqpybCQ@mail.gmail.com> <BANLkTimpTzYKLZzXAasZb52vs5sYq7Ai9A@mail.gmail.com> |
|---|---|
| Date | 2011-06-10 13:33 -0700 |
| Subject | Re: Question About Command line arguments |
| From | Dennis <daodennis@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.106.1307738018.11593.python-list@python.org> (permalink) |
On Fri, Jun 10, 2011 at 11:58 AM, Mark Phillips
<mark@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
>
>
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Question About Command line arguments Dennis <daodennis@gmail.com> - 2011-06-10 13:33 -0700
csiph-web