Amit Saurabh
Amit Saurabh

Reputation: 7

What is the difference between sys.stdin.read() and sys.stdin.readline()

Specifically, I would like to know how to give input in the case of read(). I tried everywhere but couldn't find the differences anywhere.

Upvotes: 0

Views: 1983

Answers (2)

그래그렇구나
그래그렇구나

Reputation: 21

read() recognizes each character and prints it.

But readline() recognizes the object line by line and prints it out.

Upvotes: 2

sahinakkaya
sahinakkaya

Reputation: 6056

>>> help(sys.stdin.read)
Help on built-in function read:

read(size=-1, /) method of _io.TextIOWrapper instance
    Read at most n characters from stream.
    
    Read from underlying buffer until we have n characters or we hit EOF.
    If n is negative or omitted, read until EOF.
(END)

So you need to send EOF when you are done (*nix: Ctrl-D, Windows: Ctrl-Z+Return):

>>> sys.stdin.read()
asd
123
'asd\n123\n'

The readline is obvious. It will read until newline or EOF. So you can just press Enter when you are done.

Upvotes: 3

Related Questions