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

Mark Lawrence breamoreboy at yahoo.co.uk
Wed Jan 8 18:03:53 EST 2014


On 08/01/2014 21:51, sagarnildass at 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.
>
> 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.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence




More information about the Python-list mailing list