简体   繁体   中英

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.

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

read() recognizes each character and prints it.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM