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

sagarnildass at gmail.com sagarnildass at gmail.com
Wed Jan 8 16:51:40 EST 2014


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()



More information about the Python-list mailing list