I remembered that I might need to add an else condition if the emp_num does not exist , so re sending the updated code
def get_unique_number(str(emp_id)): """ Return the unique number associated with each employee id """ out, err, rc = run("employee_details ls", timeout=600) emp_unum="" if rc != 0: return False for line in out.split('\n'): if emp_id in line: emp_unum = line.split()[0] else: print("emp_unum does not exist") return False return emp_unum PS : [Edited the above code with else condition] Regards, Ganesh On Mon, Nov 28, 2016 at 8:38 PM, Ganesh Pal <ganesh1...@gmail.com> wrote: > > > I was trying to write a function that will return me the unique number > associated with each employee id.The command has the output in the below > pattern > > Linux-Box-1# employee_details ls > List of names: > 100910bd9 s7018 > 100d60003 s7019 > 110610bd3 s7020 > 100d60002 s7021 > > > Linux-Box-1# employee_details ls | grep "s7020" | awk '{print $1}' > 100d60003 > > It's a one liner in Shell :) > > > I tried converting the same in the python style , Any better suggestion > and loop holes in the below program > > def get_unique_number(str(emp_id)): > """ Return the unique number associated with each employee id """ > out, err, rc = run("employee_details ls", timeout=600) > emp_unum="" > if rc != 0: > return False > > for line in out.split('\n'): > if emp_id in line: > emp_unum = line.split()[0] > return emp_unum > > I am on Python 2.7 and Linux OS > > Regards, > Ganesh > -- https://mail.python.org/mailman/listinfo/python-list