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 '
':
            print ''
            break
        elif newChar in '':
            if chars:
                del chars[-1]
                sys.stdout.write('')
        else:
            chars.append(newChar)
            sys.stdout.write('*')
    return str(chars)

pwd = pwd_input('password:')

这样就解决了显示问题。

原文地址:https://www.cnblogs.com/SRL-Southern/p/4780901.html