Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

.read() will read the entire contents of the file object into memory. That's fine if you're just doing `io.py < smallfile.txt`, but if the file is huge you'll run out of memory. And stdin is an infinite stream (eg. `io.py < /dev/urandom`), your Python code will never print anything. If you write `for line in sys.stdin: sys.stdout.write(line)` instead, you'll solve these problems by buffering line-by-line.


> If you write `for line in sys.stdin: sys.stdout.writte(line)` instead, you'll solve these problems by buffering line-by-line.

Except no. Then you run in to problems with a binary file that might not have a line break.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: