Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #63541

Re: python copy selected lines from one file to another using argparse or getopt

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 <python-python-list@m.gmane.org>
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 <davea@davea.name>
Subject Re: python copy selected lines from one file to another using argparse or getopt
Date Wed, 08 Jan 2014 18:57:22 -0500
References <bc99af4e-8031-477c-877f-a5460f8a09ab@googlegroups.com> <bc99af4e-8031-477c-877f-a5460f8a09ab@googlegroups.com>
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 <bc99af4e-8031-477c-877f-a5460f8a09ab@googlegroups.com>
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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5212.1389225340.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

python copy selected lines from one file to another using argparse or getopt sagarnildass@gmail.com - 2014-01-08 13:51 -0800
  Re: python copy selected lines from one file to another using argparse or getopt John Gordon <gordon@panix.com> - 2014-01-08 22:53 +0000
  Re: python copy selected lines from one file to another using argparse or getopt Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-01-08 23:03 +0000
  Re: python copy selected lines from one file to another using argparse or
 getopt Dave Angel <davea@davea.name> - 2014-01-08 18:57 -0500

csiph-web