Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #63541
| From | Dave Angel <davea@davea.name> |
|---|---|
| Subject | Re: python copy selected lines from one file to another using argparse or getopt |
| Date | 2014-01-08 18:57 -0500 |
| References | <bc99af4e-8031-477c-877f-a5460f8a09ab@googlegroups.com> <bc99af4e-8031-477c-877f-a5460f8a09ab@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.5212.1389225340.18130.python-list@python.org> (permalink) |
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 | Next — Previous in thread | Find similar | Unroll 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