toykloner
toykloner

Reputation: 3

Subprocess.call & check_output Python2.7

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

Upvotes: 0

Views: 148

Answers (1)

jfs
jfs

Reputation: 414915

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

Upvotes: 1

Related Questions