Python Forum

Full Version: Python "read -n" equivalent?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
What is the Python equivalent to the bash "read -n" command?

I understand input() pauses for user input. However, if I'm expecting x characters I'd prefer execution to continue without “Enter” once the expected number of characters have been entered.
I know very little about bash, but my understanding from this tutorial http://tldp.org/LDP/Bash-Beginners-Guide...08_02.html, is that the 'read -n' command is only interested in 'n' number of characters. So in the example, the 'read -n 1 gender' takes the first letter of the response and puts it in the variable gender. So if the user enters 'm' then ENTER, gender = 'm', if the user enters 'male' then ENTER, then gender still = 'm' since read is only looking at the first character.  In either case, the user still has to hit ENTER in order to move on.  Is my understanding correct?
(Jan-23-2017, 02:11 PM)sparkz_alot Wrote: [ -> ]I know very little about bash, but my understanding from this tutorial http://tldp.org/LDP/Bash-Beginners-Guide...08_02.html, is that the 'read -n' command is only interested in 'n' number of characters. So in the example, the 'read -n 1 gender' takes the first letter of the response and puts it in the variable gender. So if the user enters 'm' then ENTER, gender = 'm', if the user enters 'male' then ENTER, then gender still = 'm' since read is only looking at the first character.  In either case, the user still has to hit ENTER in order to move on.  Is my understanding correct?


No, that's not correct. Using 'read -n 1' eliminates the need to hit ENTER. In your example, entering 'm' continues execution.  The gender line doesn't need 'and press [ENTER]'. You can easily see what happens with a one-line bash script.

   echo 'Enter one character.'; read

The above command requires [Enter]. However, the command below does not.

   echo 'Enter one character.'; read -n 1

The Python equivalent of 'read' is input(). But what's the equivalent of 'read -n 1'?  This is probably something quite basic in Python but I'm just not seeing it.
This is not from command line, but how you would do it in a script


with open 'myfile.txt' as f:
    x = f.read(8)
    print(x)
x should be first 8 characters
From what I can tell, there's no cross-platform solution to this problem. Python doesn't have anything built-in for it. What platform(s) do you need to support?
Hello!
See https://docs.python.org/3.5/library/os.h...t-creation

I didn't need this yet.
wavic, I don't think that answers the question.
As pointed out bye @micseydel this can be platform(s) depended.
To get it to work like "read -n" command,
has to listen to keyboard event(this is usually called to Hook keyboard events).
There are modules like keyboard,that could do this.
Quote:Listen and sends keyboard events.
Works with Windows and Linux (requires sudo).

I have used pyhooked before,but that is a Windows solution.
And i see that someone has ported it to Linux(blog).

pynput 1.2 look nice,and is updated and cross-platform.
Quote:Monitoring the keyboard
Use pynput.keyboard.Listener like this:
(Jan-23-2017, 10:26 PM)snippsat Wrote: [ -> ]As pointed out bye @micseydel this can be platform(s) depended.
To get it to work like "read -n" command,
has to listen to keyboard event(this is usually called to Hook keyboard events).
There are modules like keyboard,that could do this.

Thank you, I assumed I was missing something basic. But thanks to you and misceydel I see I should try another tack for my Linux solution.

I'll try one or more of the modules.