python raw input用法_如何在 python raw_input 中使用 tab 键补全?

本文介绍了如何在Python中实现命令行补全功能,特别是在Mac和Windows上。通过导入readline模块或者在没有readline时使用pyreadline模块,实现了Tab键的补全功能。同时,代码示例展示了如何编写complete_cd函数来根据用户输入补全目录路径。对于Mac系统,需要额外处理libedit库的情况。
摘要由CSDN通过智能技术生成

http://stackoverflow.com/ques...

这段代码写得不错,如果要补全第二个参数要自己写 complete 函数类似于下面的代码。

def complete_cd(self, *args):

cwd = CURRENT_PATH

results = [c for c in os.listdir(cwd) if c.startswith(args[0][0])]

return results

ps:官方文档有说:

Note The underlying Readline library API may be implemented by the libedit library instead of GNU readline. On MacOS X the readline module detects which library is being used at run time.

The configuration file for libedit is different from that of GNU readline. If you programmatically load configuration strings you can check for the text “libedit” in readline.__doc__ to differentiate between GNU readline and libedit.

所以在 MAC 和 Windows 上面不适用

如果要在全平台使用(没有测试 Windows),使用如下加载代码

try:

import readline

except ImportError:

try:

import pyreadline as readline

# throw open a browser if we fail both readline and pyreadline

except ImportError:

import webbrowser

webbrowser.open("http://ipython.scipy.org/moin/PyReadline/Intro#line-36")

# throw open a browser

#pass

else:

import rlcompleter

if(sys.platform == 'darwin'):

readline.parse_and_bind ("bind ^I rl_complete")

else:

readline.parse_and_bind("tab: complete")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值