Python Forum
Python "read -n" equivalent?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python "read -n" equivalent?
#1
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.
Reply
#2
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?
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
(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.
Reply
#4
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
Reply
#5
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?
Reply
#6
Hello!
See https://docs.python.org/3.5/library/os.h...t-creation

I didn't need this yet.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
wavic, I don't think that answers the question.
Reply
#8
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:
Reply
#9
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Equivalent Python code from VBA Mishal0488 2 778 Apr-19-2024, 10:32 AM
Last Post: masonsbore
  Python C++ Template Equivalent deanhystad 7 3,396 May-04-2021, 07:45 PM
Last Post: deanhystad
  Hi Guys, please help me to write SAS macro parameter equivalent code in Python Manohar9589 2 2,597 Jun-14-2020, 05:07 PM
Last Post: Larz60+
  python equivalent to MATLAB xcov chai0404 2 3,878 Apr-02-2020, 10:29 PM
Last Post: chai0404
  Python equivalent of Matlab code kwokmaster 1 3,454 Mar-25-2020, 10:14 PM
Last Post: j.crater
  Equivalent of 'bwareafilt' for Python Mighty 1 3,602 Feb-05-2020, 10:32 PM
Last Post: Marbelous
  Convert a mongo db scrip to python equivalent Herath 1 2,190 Aug-20-2019, 02:28 PM
Last Post: fishhook
  SAS Equivalent of Macro variable in Python AI_ML_Neophyte 4 11,657 Mar-29-2019, 08:14 AM
Last Post: FelixS
  Python equivalent to sed [solved] cygnus_X1 2 11,400 Sep-24-2018, 10:13 PM
Last Post: cygnus_X1
  equivalent commands saeed_balk 2 2,687 Sep-23-2018, 05:44 AM
Last Post: saeed_balk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020