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

S

sagarnildass

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.

Also the user will have an option to exclude any word.

(e.g Suppose the user searches for the word "exception" and want to exclude the word "abc", then the code will only copy the lines which has "exception" in it but not "abc").

Now all the work will be done from the command prompt.

The input would be:

file.py test.txt(input file) test_mod.txt(output file) -e abc(exclude word denoted by -e)-s exception(search word denoted by -s)
Now the user will have an option to enter multiple exclude words and multiple search words.

Now so far I have achieved that the input format is:

file.py test.txt test_mod.txt abc exception".
This excludes the word "abc" and search for "exception".

But I don't know how to:

Include multiple search word and exclude words
How to denote them by -e and -s. I have seen the argparse and the getopt tutorial. But there's no tutorial on this specific topic.
Please can somebody help me by modifying my code or write a new one?

Here's my code as of now:

#/Python33

import sys
import os




def main(): #main method

try:

f1 = open(sys.argv[1], 'r') #takes the first input file in command line
found = False
user_input1 = (sys.argv[3]) #takes the word which is to be excluded.
user_input2 = (sys.argv[4]) #takes the word which is to be included.
if sys.argv[1] == sys.argv[2]:
f1.close()
sys.exit('\nERROR!!\nThe two file names cannot be the same.')

if sys.argv[3] != sys.argv[4]:

for line in f1:

if user_input1 in line or user_input2 in line:

f2 = open(sys.argv[2], 'a')

if user_input1 in line:
if user_input2 in line:
pass
elif user_input2 in line:
f2.write(line)
found = True
f2.close()


if not found:
print("ERROR: The Word couldn't be found.")



f1.close()


if sys.argv[3] == sys.argv[4]:
f1.close()
sys.exit('\nERROR!!\nThe word to be excluded and the word to be included cannot be the same.')



except IOError:
print('\nIO error or wrong file name.')
except IndexError:
print('\nYou must enter 5 parameters.') #prevents less than 5 inputs which is mandatory
except SystemExit as e: #Exception handles sys.exit()
sys.exit(e)


if __name__ == '__main__':
main()
 
J

John Gordon

In said:
But I don't know how to:
Include multiple search word and exclude words
How to denote them by -e and -s. I have seen the argparse and the getopt
tutorial. But there's no tutorial on this specific topic.

This should help you get started:

import argparse

parser = argparse.ArgumentParser()

parser.add_argument('-search', '-s', help='a word to search',
action='append', required=True)
parser.add_argument('-exclude', '-e', help='a word to exclude from search',
action='append')
parser.add_argument('input_file')
parser.add_argument('output_file')

args = parser.parse_args()

Assuming the user provided correct arguments, the args object will have
these attributes:

args.search - a list of words to search for.
args.exclude - a list of words to exclude. Can be None.
args.input_file - the input file name.
args.output_file - the output file name.
 
M

Mark Lawrence

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.

Also the user will have an option to exclude any word.

(e.g Suppose the user searches for the word "exception" and want to exclude the word "abc", then the code will only copy the lines which has "exception" in it but not "abc").

Now all the work will be done from the command prompt.

The input would be:

file.py test.txt(input file) test_mod.txt(output file) -e abc(exclude word denoted by -e)-s exception(search word denoted by -s)
Now the user will have an option to enter multiple exclude words and multiple search words.

Now so far I have achieved that the input format is:

file.py test.txt test_mod.txt abc exception".
This excludes the word "abc" and search for "exception".

But I don't know how to:

Include multiple search word and exclude words
How to denote them by -e and -s. I have seen the argparse and the getopt tutorial. But there's no tutorial on this specific topic.
Please can somebody help me by modifying my code or write a new one?

If you can use third party modules I suggest you look at docopt, it's
available on pypi.
 
D

Dave Angel

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


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top