3 ms·
It’s pretty easy. You have three basic streams: 1. Stdin - just iterate through sys.stdin 2. Stdout - regular printing will go there 3. Stderr - print errors
by KMnO4 4y ago
It’s pretty easy. You have three basic streams:
1. Stdin - just iterate through sys.stdin
2. Stdout - regular printing will go there
3. Stderr - print errors here eg with print(…, file=sys.stderr)
And then beyond that as long as your script gets invoked by the interpreter (Ie #!/usr/bin/env python) everything will “just work”.
- IgorPartola 4y agoDon’t you also have to keep in mind how often you flush outputs/how you buffer? Encoding? Handle EOF correctly? Not saying it’s hard but also it’s not 100% covered by what you said.
- markrages 4y agoThose are advanced topics and you can look them up if you need them. Generally, Python does the right thing by default for scripting use: line buffered, system encoding, EOF handled naturally by the iterator protocol.
- gnubison 4y agoAnd preferably use fileinput for the stdin so that you can name files on the command line as well
- Calzifer 4y agoAnd avoid seek. Pipes are not random access. I once tried to use a python library to convert a file from stdin but it failed on a f.seek(0) the library added 'just in case' in the beginning.