'Tab autocomplete python3 conversion

I have built this auto completion command line script in python, it will execute the "ls" command in linux enviroment, convert the resulting string into a list and assigning that list to the tab auto complete function.

#!/usr/bin/env python

import readline, subprocess

readline.parse_and_bind("tab: complete")


def complete(text,state):

    result = subprocess.check_output("ls")
    volcab = result.split("\n")
    results = [x for x in volcab if x.startswith(text)] + [None]
    return results[state]

readline.set_completer(complete)

line = input('>> ')

The problem is that when i try to convert it in python 3 is not going to work because instead of returning a regular list structure = ['element1', 'element2', 'element3'], python3 will return the list in binary form [b'element1',b'element2',b'element3'] ... I couldn't separate the elements of the list with this structure, so the auto completer did not work.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source