def fcomplete(text, state):
    """Readline completion function: Filenames"""

    text = os.path.expanduser(text)
    head, tail = os.path.split(text)

    search_dir = os.path.join('.', head)
    candidates = [s for s in os.listdir(search_dir) if s.startswith(tail)]

    if state >= len(candidates):
        return None

    if len(candidates) == 1:
        fn = os.path.join(head, candidates[0])
        if not os.path.isdir(fn):
            return fn + ' '
        return fn + '/'

    return os.path.join(head, candidates[state])
Readline completion function: Filenames