使用用户输入文件路径自动完成 [英] filepath autocompletion using users input

查看:195
本文介绍了使用用户输入文件路径自动完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(蟒蛇)

我期待抢用户输入文件路径。看来pretty基本的,但我似乎无法得到的readline或rlcompleter工作。

I'm looking to grab a users input for a filepath. It seems pretty basic, but I can't seem to get readline or rlcompleter working.

pretty多少:
变量的raw_input =('文件路径:'),然后将文件路径具有自动完成功能,例如它会在一个外壳。

Pretty much: variable = raw_input(' Filepath: ') and then the filepath has autocomplete functions like it would in a shell.

我不局限于蟒蛇,我愿意用任何语言,只要我可以设置一个变量的文件路径,并使用自动填充功能抢文件路径。

I'm not restricted to python, I'm willing to use any language so long as I can set a variable as the filepath and grab the filepath using autocomplete functionality.

我已经看到了这个:选项卡中完成Python的的raw_input()这让我得到一个什么样寻找一个想法,虽然问题是,它需要在文件路径前面的命令,如额外。我需要设置变量的文件路径。你会觉得​​这是pretty简单,但我还没有发现太多的任何地方,而且我已经找到了少数人不正是我一直在寻找。

I've seen this: Tab completion in Python's raw_input() which helped me get an idea of what to look for, although the problem was that it required a command in front of the filepath such as "extra". I need to set the variable as the filepath. You'd think it'd be pretty simple, but I haven't found much on it anywhere, and the few that I have found weren't exactly what I was looking for.

在bash中有,可以在命令行中运行读-e命令,但它不是在脚本这是奇怪的认可。这正是我一直在寻找,只要它可以利用的脚本里面的变量等于设置为自动完成的文件路径。

In bash there was a read -e command that can be run in a command line, but it's not recognized in a script which was odd. It's exactly what I was looking for, if only it could be utilized inside of a script to set the variable equal to the autocompleted filepath.

推荐答案

这样呢?

import readline, glob
def complete(text, state):
    return (glob.glob(text+'*')+[None])[state]

readline.set_completer_delims(' \t\n;')
readline.parse_and_bind("tab: complete")
readline.set_completer(complete)
raw_input('file? ')

这篇关于使用用户输入文件路径自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆