Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'elif': 0.04; 'main()': 0.05; 'sys': 0.05; 'args': 0.07; 'tests,': 0.07; '__name__': 0.09; 'argument,': 0.09; 'fred': 0.09; 'kurt,': 0.09; 'received:mail- bw0-f46.google.com': 0.09; 'tty': 0.09; 'this:': 0.10; 'def': 0.12; 'am,': 0.14; 'wrote:': 0.14; '"__main__":': 0.16; '#this': 0.16; 'arg': 0.16; 'bottom.': 0.16; 'main():': 0.16; 'phillips': 0.16; 'received:209.85.214.46': 0.16; 'redirection,': 0.16; 'stdin': 0.16; 'argument': 0.16; 'command': 0.19; 'thanks,': 0.19; 'header:In-Reply-To:1': 0.21; 'fri,': 0.23; 'subject:Question': 0.25; 'changed': 0.25; 'url:mailman': 0.26; 'string': 0.26; 'script': 0.27; 'message-id:@mail.gmail.com': 0.28; 'received:209.85.214': 0.28; 'import': 0.29; 'url:listinfo': 0.30; "didn't": 0.31; 'print': 0.31; 'seem': 0.32; 'does': 0.33; 'to:addr:python-list': 0.33; 'skip:# 10': 0.34; 'weird': 0.35; 'skip:. 10': 0.36; 'skip:o 20': 0.37; 'received:google.com': 0.37; 'received:209.85': 0.37; 'checks': 0.37; 'url:python': 0.38; 'url:org': 0.38; 'subject:: ': 0.38; 'received:209': 0.39; 'to:addr:python.org': 0.39; 'results': 0.60; 'below': 0.61; 'here': 0.66; 'subject:About': 0.68; 'subject:line': 0.73; 'dennis': 0.77; '11:58': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type; bh=mIKniYgfSWw9lFpFmL4sP5WSHAq6VoaYLVyDDd90P0I=; b=wZEBjxszqEaEnM0/rhp6m2JBc8WaFwkmKBk0qGZH35USQ/ZmHbG3aBqJfxOcfNih/+ ck3iHMyJ64SH+t5CKpyOyA0jl/ukoSwoBf/lou+5kUnbeoL4k91xUxm33mGELhZKOeXX 7SNST9TUdot7cox84mkbYhkliecRv3+Jppjc0= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; b=lnPJLhM4XsEvv0ldgu6lZbqgefcj9DK4ylf948lBmRSwP5+VtkImCVLH7CLtYH4oek FcfaSma1yy9l+iorCuOhCaUHxPTYzmh7tf3yPx1d1rnpQ4EgC+ONvOPPd933KZohCvJE gZQ8vzXkm+Q0cm9oPDiXqRUXzEYD2U3Shxg0k= MIME-Version: 1.0 In-Reply-To: References: <4DF25738.2020704@mrabarnett.plus.com> Date: Fri, 10 Jun 2011 13:33:36 -0700 Subject: Re: Question About Command line arguments From: Dennis To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 68 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1307738018 news.xs4all.nl 49182 [::ffff:82.94.164.166]:38186 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:7400 On Fri, Jun 10, 2011 at 11:58 AM, Mark Phillips 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 > >