2015/9/3 Python密码输入屏蔽字符

在使用Python的过程中,想输入账号和密码,但是密码会随着输入显示在屏幕上,为了解决这个问题需要用到msvcrt模块

这里是使用代码

import  msvcrt, sys

def pwd_input(a):
    print str(a),
    chars = []
    while True:
        newChar = msvcrt.getch()
        if newChar in '\r\n':
            print ''
            break
        elif newChar in '\b':
            if chars:
                del chars[-1]
                sys.stdout.write('\b\b')
        else:
            chars.append(newChar)
            sys.stdout.write('*')
    return str(chars)

pwd = pwd_input('password:')

这样就解决了显示问题。

posted @ 2015-09-03 22:43  #SRL  阅读(823)  评论(0编辑  收藏  举报