3 ms·
Question: I see all this discussion about buffering and encoding and such. In python, this seems to work: import sys sys.stdout.write(sys.stdin.read()) What i
by brilee 13y ago
Question: I see all this discussion about buffering and encoding and such.
In python, this seems to work:
import sys
sys.stdout.write(sys.stdin.read())
What is being abstracted away/handled by python that I'm not seeing?
- cbsmith 13y agoNothing beyond an automatic flush/close of stdout on exit. The python code above is also reading the entire file in to memory before writing it out to disk, which isn't exactly wise. Aside from the addition of a flush at the end, the correct Java code for this is simply a loop with an exit test over a read() & write() though, which is really no different from the proper Python code.
- sjy 13y ago.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.
- cbsmith 13y ago> 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.
- cbsmith 13y agoThere seems to be confusion on how to write this properly, so don't feel bad about missing this (or alternatively feel bad for all of humanity). I believe this Java code I posted elsewhere handles things just fine: https://gist.github.com/cbsmith/9755809 https://gist.github.com/cbsmith/9755809