0

I was reading about the subprocess module and have gotten very confused. Under my search function, I wanted to use subprocess.check_output to return a printed error message of my chosing when grep failed to find the users input in the text file. How would I go about doing this? Using Python 2.7

import subprocess

def menu():
    usr = raw_input("Press 1 if you are creating a new entry, press 2 for search or 3 to exit")
    if usr == '1':
        collect()
    if usr == '2':
        search()
    if usr == '3':
        sys.exit()
    if usr == '4':
        subprocess.call(['vim', '-c', '10', 'book.txt'])
def search():
    inp = raw_input("Please enter a name:")
    subprocess.call(['rgrep', '-e', inp])
    search()
def collect():
    def do_global():
        global name, ac, number
        name = raw_input("Name")
        ac = raw_input("Area Code")
1
  • You may be interested in higher level wrappers around subprocess like the sh module. Commented Jan 5, 2014 at 3:14

1 Answer 1

1
if subprocess.call(['rgrep', '-e', inp]) != 0: # rgrep failed
   print("error message of your choosing")

To find out what exit status grep may produce, see its man page

Sign up to request clarification or add additional context in comments.

2 Comments

Sebastian, where would I find documentation on the purpose of !=0 that you provided in your answer. I'd like to read more on this myself. Thank you
@toykloner: you could start at wikipedia article on "Exit status".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.