Path: csiph.com!usenet.pasdenom.info!news.albasani.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'argument': 0.05; 'output': 0.05; 'subject:file': 0.07; 'append': 0.09; 'arguments': 0.09; 'filenames': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:using': 0.09; 'python': 0.11; 'jan': 0.12; 'exists,': 0.16; 'indent': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:argparse': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'trying': 0.19; 'header:User-Agent:1': 0.23; 'file.': 0.24; 'looks': 0.24; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'words': 0.29; "doesn't": 0.30; 'mode': 0.30; 'specified': 0.30; 'especially': 0.30; 'code': 0.31; 'lines': 0.31; 'factor': 0.31; 'searches': 0.31; 'txt': 0.31; 'file': 0.32; 'probably': 0.32; 'handled': 0.32; 'another': 0.32; 'checking': 0.33; 'subject:from': 0.34; 'something': 0.35; 'objects': 0.35; 'subject:one': 0.36; 'should': 0.36; 'two': 0.37; 'lists.': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'john': 0.61; 'you.': 0.62; 'email addr:gmail.com': 0.63; 'size.': 0.65; 'equals': 0.68; 'containing': 0.69; 'subjectcharset:utf-8': 0.72; 'protect': 0.79; 'received:myvzw.com': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dave Angel Subject: =?UTF-8?Q?Re:_python_copy_selected_lines_from_one_?= =?UTF-8?Q?file_to_another_using_argparse_or=0A_getopt?= Date: Wed, 08 Jan 2014 18:57:22 -0500 References: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: 96.sub-70-208-129.myvzw.com In-Reply-To: User-Agent: Groundhog Newsreader for Android 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: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1389225340 news.xs4all.nl 2936 [2001:888:2000:d::a6]:53577 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:63541 On Wed, 8 Jan 2014 13:51:40 -0800 (PST), sagarnildass@gmail.com wrote: > I am trying to write a program in python which searches for user specified words in a txt file and copies the selected lines containing that word into another file. John Gordon has given you a good start on argument parsing. So let's start with some general comments. Please indent by 4 columns. Please factor your code into multiple functions so it's readable, especially by you. Please learn that complementary conditions are best handled by else not by another if. So the arguments to your main function should probably be two file objects and two lists. The function should call another one for each list, in a loop that looks something like for line in infile: if check_include (includes, line and not check_exclude (excludes, line): dosomethingwith line By the way checking the 2 filenames for equals doesn't begin to protect against trashing some file. Better to check if the output file exists, perhaps by opening in append mode and checking size. -- DaveA