ich benutze das CMD-Modul und würde gerne für das Argument eines bestimmten Befehls (hier 'xxx') eine automatische Pfadvervollständigung mittels <tab> realisieren.
Z.B. "xxx /home/pyus<tab>" wird zu "xxx /home/pyuser/". Dazu benutze ich folgenden Code:
Code: Alles auswählen
def _listdir(self, root):
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):
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)]
if len(res) > 1 or not os.path.exists(path):
return res
if os.path.isdir(path):
return [os.path.join(path, p) for p in self._listdir(path)]
return [path + ' ']
def complete_xxx(self, text, line, begidx, endidx):
if not text:
return self._complete_path()
return self._complete_path(line.split()[1])
Oder erfinde ich damit gerade das Rad neu und es gibt eine viel einfachere Lösung?
Danke schon mal!