Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python \b而文字包装不会返回_Python_Python 3.x - Fatal编程技术网

Python \b而文字包装不会返回

Python \b而文字包装不会返回,python,python-3.x,Python,Python 3.x,我有一些代码发送\b,发送一个空格,然后发送另一个\b 目前,它会很好地删除最后一个字符,但不会在文本换行后删除,如下所示: 这不是我想要的,我希望它删除第一行的字符,而不是在新的一行开始时停止。 这是我目前的代码: import sys import msvcrt def getpass(prompt='Password: ', stream=None): """Prompt for password with echo off, using Windows getch()."""

我有一些代码发送
\b
,发送一个空格,然后发送另一个
\b

目前,它会很好地删除最后一个字符,但不会在文本换行后删除,如下所示:

这不是我想要的,我希望它删除第一行的字符,而不是在新的一行开始时停止。 这是我目前的代码:

import sys
import msvcrt
def getpass(prompt='Password: ', stream=None):
    """Prompt for password with echo off, using Windows getch()."""
    for c in prompt:
        msvcrt.putwch(c)
    pw = ""
    typed = 0
    while True:
        c = msvcrt.getwch()
        if c == '\r' or c == '\n':
            break
        if c == '\003':
            raise KeyboardInterrupt
        if c == '\b':
            pw = pw[:-1]
            if not len(pw) == 0:
                typed = 1
                msvcrt.putwch("\b")
                msvcrt.putwch("\u0020")
                msvcrt.putwch("\b")
            elif typed == 1:
                typed = 0
                msvcrt.putwch("\b")
                msvcrt.putwch("\u0020")
                msvcrt.putwch("\b")
        else:
            pw = pw + c
            msvcrt.putwch("*")
            typed = 1
    msvcrt.putwch('\r')
    msvcrt.putwch('\n')
    return pw
问题在于:

        if c == '\b':
            pw = pw[:-1]
            if not len(pw) == 0:
                typed = 1
                msvcrt.putwch("\b")
                msvcrt.putwch("\u0020")
                msvcrt.putwch("\b")

那么,你的问题是什么?看起来这是终端模拟器的一个特性(我在Linux上的X终端中看到了相同的行为)。顺便说一句,
“\u0020”
只是
,不需要掩盖打印空白的事实。这可以使用Windows控制台API或curses来完成。@DYZ我不认为您可以在
msvcrt.putwch()中放置普通空格。