Search code examples
pythonpython-3.xsystem

Password writing system: standard output no longer works


I'm currently working on a personnal project and I try to create a password function. So I wrote this script :

import getch
import sys

print("votre mot de passe : ", end="")
passwor = ''
while True:
    x = getch.getch()
    if x == '\n':
        break
    sys.stdout.write("*")
    passwor += x

print('\n'+passwor)

But I'm stucking on a problem : nothing appear on my output console It apear the problem come from end="" argument from print and sys.stdout.write(""). I try to remove both and it working proprely (I change sys.stdout.write("") by print("*")). I made some research and I found a solution : if I run my script like : python3 -u <my_script> it works But I want to call this function on an another file... so it should be great if I can run my scipt without -u argument... Does it exist a solution ? Just for information : [os:debian(wsl),version:python3.7.3]


Solution

  • If you are using Python 3.3 or later, you can write print("votre mot de passe : ", end="", flush=True) and it will automatically force Python to write everything in the output buffer to the terminal. You can also flush the output buffer manually by writing sys.stdout.flush() after printing.