Python – how to hide typed characters

Hello! Sometimes you want to hide the characters you enter – when entering a password, for example (otherwise it will be like in an immortal movie Sneakers” 🙂 ).
At least now, in the process of working on a simple FTP-client, which will only be able to, that:

  • Get directory contents
  • Create directory
  • Go to the specified directory
  • Delete file
  • Delete directory
  • Get file size
  • Load file on FTP
  • Download file from FTP

– moreover, all this in the style of the late 80s is a command line, and no windows! Only so, only hardcore! So – I ran into the problem of hiding the entered characters in the process of entering the password for FTP. How to solve this problem?

It is for this case that Python has a built-in getpass library, documentation for which is available here . The logic of the work is simple – display a data request, and hide the input itself. Elegantly. Simply.
Sample code is below:

  1. import getpass
  2. password = getpass.getpass('Pass: ')
  3. if password == '1234':
  4.   print('Ok!')
  5. elif password != '1234':
  6.   print('Problem :( ')

And here is an example of the code:

Show in normal working form – the problem is that we run the code in a virtual cloud machine, so it is impossible to intercept the input stream. At the same time, I promise – in local mode everything will be great.

Thanks for your attention! Do not forget to like, and click on the banner 🙂