Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!cs.uu.nl!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!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; 'else:': 0.03; '16,': 0.03; 'args': 0.04; 'error:': 0.05; 'sys': 0.05; 'filename': 0.07; 'parser': 0.07; 'valueerror:': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'script,': 0.09; 'throws': 0.09; 'unpack': 0.09; 'valueerror': 0.09; 'def': 0.10; '"-":': 0.16; 'argparse': 0.16; 'contextlib': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'subject:whitespaces': 0.16; 'wrote:': 0.17; 'tend': 0.17; 'yield': 0.17; 'input': 0.18; 'skip:" 30': 0.20; 'import': 0.21; 'header:User-Agent:1': 0.26; '(most': 0.27; 'right.': 0.27; 'replace': 0.27; 'skip:@ 10': 0.27; 'header:X-Complaints-To:1': 0.28; 'kumar': 0.29; 'methods.': 0.29; 'code': 0.31; 'file': 0.32; 'print': 0.32; 'traceback': 0.33; 'to:addr:python-list': 0.33; 'subject:?': 0.35; 'something': 0.35; 'received:org': 0.36; 'skip:p 20': 0.36; 'subject:: ': 0.38; 'mean': 0.38; 'skip:l 20': 0.38; 'to:addr:python.org': 0.39; 'takes': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'more': 0.63; 'subject:Any': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Any algorithm to preserve whitespaces? Date: Wed, 23 Jan 2013 23:04:02 +0100 Organization: None References: <50FAEE23.5030107@lightbird.net> <50FFBCC5.5020506@davea.name> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084a1ab.dip.t-dialin.net User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 60 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1358979213 news.xs4all.nl 6928 [2001:888:2000:d::a6]:37851 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:37510 Santosh Kumar wrote: > Yes, Peter got it right. > > Now, how can I replace: > > script, givenfile = argv > > with something better that takes argv[1] as input file as well as > reads input from stdin. > > By input from stdin, I mean that currently when I do `cat foo.txt | > capitalizr` it throws a ValueError error: > > Traceback (most recent call last): > File "/home/santosh/bin/capitalizr", line 16, in > script, givenfile = argv > ValueError: need more than 1 value to unpack > > I want both input methods. You can use argparse and its FileType: import argparse import sys parser = argparse.ArgumentParser() parser.add_argument("infile", type=argparse.FileType("r"), nargs="?", default=sys.stdin) args = parser.parse_args() for line in args.infile: print line.strip().title() # replace with your code As this has the small disadvantage that infile is opened immediately I tend to use a slight variation: import argparse import sys from contextlib import contextmanager @contextmanager def xopen(filename): if filename is None or filename == "-": yield sys.stdin else: with open(filename) as instream: yield instream parser = argparse.ArgumentParser() parser.add_argument("infile", nargs="?") args = parser.parse_args() with xopen(args.infile) as instream: for line in instream: print line.strip().title()