Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4342552
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:38:00+00:00 2026-05-21T11:38:00+00:00

i know i can do this to get the effect of tab completion in

  • 0

i know i can do this to get the effect of tab completion in python sure.

import readline
COMMANDS = ['extra', 'extension', 'stuff', 'errors',
            'email', 'foobar', 'foo']

def complete(text, state):
    for cmd in COMMANDS:
        if cmd.startswith(text):
            if not state:
                return cmd
            else:
                state -= 1

readline.parse_and_bind("tab: complete")
readline.set_completer(complete)
raw_input('Enter section name: ')

I am now interested in doing tab completion with directories. (/home/user/doc >tab)

How would i go about doing such a task?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T11:38:00+00:00Added an answer on May 21, 2026 at 11:38 am

    Here is a quick example of how to perform incremental completion of file system paths. I’ve modified your example, organizing it into a class where methods named complete_[name] indicate top-level commands.

    I’ve switched the completion function to use the internal readline buffer to determine the state of the overall completion, which makes the state logic a bit simpler. The path completion is in the _complete_path(path) method, and I’ve hooked up the extra command to perform path completions on its arguments.

    I’m sure the code could be further simplified but it should provide you a decent starting point:

    import os
    import re
    import readline
    
    COMMANDS = ['extra', 'extension', 'stuff', 'errors',
                'email', 'foobar', 'foo']
    RE_SPACE = re.compile('.*\s+$', re.M)
    
    class Completer(object):
    
        def _listdir(self, root):
            "List directory 'root' appending the path separator to subdirs."
            res = []
            for name in os.listdir(root):
                path = os.path.join(root, name)
                if os.path.isdir(path):
                    name += os.sep
                res.append(name)
            return res
    
        def _complete_path(self, path=None):
            "Perform completion of filesystem path."
            if not path:
                return self._listdir('.')
            dirname, rest = os.path.split(path)
            tmp = dirname if dirname else '.'
            res = [os.path.join(dirname, p)
                    for p in self._listdir(tmp) if p.startswith(rest)]
            # more than one match, or single match which does not exist (typo)
            if len(res) > 1 or not os.path.exists(path):
                return res
            # resolved to a single directory, so return list of files below it
            if os.path.isdir(path):
                return [os.path.join(path, p) for p in self._listdir(path)]
            # exact file match terminates this completion
            return [path + ' ']
    
        def complete_extra(self, args):
            "Completions for the 'extra' command."
            if not args:
                return self._complete_path('.')
            # treat the last arg as a path and complete it
            return self._complete_path(args[-1])
    
        def complete(self, text, state):
            "Generic readline completion entry point."
            buffer = readline.get_line_buffer()
            line = readline.get_line_buffer().split()
            # show all commands
            if not line:
                return [c + ' ' for c in COMMANDS][state]
            # account for last argument ending in a space
            if RE_SPACE.match(buffer):
                line.append('')
            # resolve command to the implementation function
            cmd = line[0].strip()
            if cmd in COMMANDS:
                impl = getattr(self, 'complete_%s' % cmd)
                args = line[1:]
                if args:
                    return (impl(args) + [None])[state]
                return [cmd + ' '][state]
            results = [c + ' ' for c in COMMANDS if c.startswith(cmd)] + [None]
            return results[state]
    
    comp = Completer()
    # we want to treat '/' as part of a word, so override the delimiters
    readline.set_completer_delims(' \t\n;')
    readline.parse_and_bind("tab: complete")
    readline.set_completer(comp.complete)
    raw_input('Enter section name: ')
    

    Usage:

    % python complete.py 
    Enter section name: ext<tab>
    extension extra
    Enter section name: extra foo<tab>
    foo.py foo.txt foo/
    Enter section name: extra foo/<tab>
    foo/bar.txt foo/baz.txt
    Enter section name: extra foo/bar.txt
    

    Update It will complete paths from the root if the user types /:

    % python complete.py
    Enter section name: extra /Use<tab>
    /Users/.localized  /Users/Shared/  /Users/user1 /Users/user2
    Enter section name: extra /Users/use<tab>
    /Users/user1  /Users/user2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know you can do this to get vertical text in a tab header:
I know you can do this.GetType().FullName To get My.Current.Class But what can I call
Very frustrated here. Can't get this to work and don't know why. I am
I can't even get off the ground.I DO KNOW HOW TO ADD REFERENCES, (this.mshtml
I know this is rather laughable, but I can't seem to get simple C++
I know this can be done and i have seen it done using some
I know you can do this <%= Request.Form[0] %> But how do you do
I know you can do this with virtual attributes, but what if the column
I know I can use this way to find the most frequent element in
I know I can do this... RegExp.prototype.xtend = function(){ console.log(123) } y = new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.