Python Forum
Python "read -n" equivalent? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python "read -n" equivalent? (/thread-1724.html)



Python "read -n" equivalent? - rhubarbpieguy - Jan-22-2017

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.


RE: Python "read -n" equivalent? - sparkz_alot - Jan-23-2017

I know very little about bash, but my understanding from this tutorial http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_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?


RE: Python "read -n" equivalent? - rhubarbpieguy - Jan-23-2017

(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/html/sect_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.


RE: Python "read -n" equivalent? - Larz60+ - Jan-23-2017

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


RE: Python "read -n" equivalent? - micseydel - Jan-23-2017

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?


RE: Python "read -n" equivalent? - wavic - Jan-23-2017

Hello!
See https://docs.python.org/3.5/library/os.html#file-object-creation

I didn't need this yet.


RE: Python "read -n" equivalent? - micseydel - Jan-23-2017

wavic, I don't think that answers the question.


RE: Python "read -n" equivalent? - snippsat - Jan-23-2017

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:



RE: Python "read -n" equivalent? - rhubarbpieguy - Jan-24-2017

(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.