osdir.com

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

To check for empty string after a portion of the string in python 3.6


Again, _please_ use just one list: tutor or python-list. I've have 
directed replies to the tutor list. - Cameron

On 04Dec2018 04:05, srinivasan <srinivasan.rns at gmail.com> wrote:
>Thanks a lot for your quick responses, Could you please let me know
>when the device is not enabled I get the error " I get the below error
>saying "IndexError: list index out of range""

>Code snippet:
[...]
>res = self._helper.execute_cmd_output_string(cmd)
>print("The value of res!!!!", res)
>res = self._helper.execute_cmd_output_string(cmd).split("\n", 2)
>print("the value!!!!!!!!!!!!", res)
>print("the value!!!!!!!!!!!!", res[1])
>if __name__ == "__main__":
>    m = Bt()
>    print(m.bluetooth_scan())
>
>1. Output when device is enabled:
>The value of res!!!! Scanning ...
>64:A2:F9:06:63:79 OnePlus 6
>the value!!!!!!!!!!!! ['Scanning ...', '\t64:A2:F9:06:63:79\tOnePlus 6']
>the value!!!!!!!!!!!! 64:A2:F9:06:63:79 OnePlus 6
>None

The "None" is because your bluetooth_scan method has no return value (I 
think), which in Python it means it returns None.

>2. Output when device is not enabled
>When the device is not connected, I get the below error saying
>"IndexError: list index out of range"
>The value of res!!!! Scanning ...
>Traceback (most recent call last):
>the value!!!!!!!!!!!! ['Scanning ...']
>  File "/home/srinivasan/Downloads/bt_tests/qa/test_library/Bt.py",
>line 96, in <module>
>    print(m.bluetooth_scan())
>  File "/home/srinivasan/Downloads/bt_tests/qa/test_library/Bt.py",
>line 74, in bluetooth_scan
>    print("the value!!!!!!!!!!!!", res[1])
>IndexError: list index out of range

Well, you've printed your list:

  ['Scanning ...']

It is a single element list. So it has an element at index 0. There is 
no element 1. Therefor Python raises an IndexError if you try to access 
that element.

Cheers,
Cameron Simpson <cs at cskk.id.au>