Input/output redirection changes where a command reads or writes without changing its core job. At a command prompt, `<` supplies standard input from a file, while `>` sends standard output to a file. This makes it possible to connect small, specialized programs. section 11.1.4
Replace text from one file into another
`python replace.py zero 0 < infile > outfile` reads `infile`, replaces occurrences of `zero` with `0`, and writes the result to `outfile`. The two words after the script name remain command-line arguments; the redirected file is not an argument. section 11.1.4
Listing 11.3 File replace.py
import sys def main(): contents = sys.stdin.read() #1 sys.stdout.write(contents.replace(sys.argv[1], sys.argv[2])) #2 if __name__ == "__main__": main()
The script reads `sys.stdin` to end-of-file and writes its transformed contents to `sys.stdout`. `print()` also writes to standard output by default. section 11.1.4
The same streams support a few useful variations:
- Use `>>` instead of `>` to append output rather than overwrite the destination file. section 11.1.4
- Use a pipe (`|`) to feed one command’s output into another command’s input. section 11.1.4
- Without redirected input, a script reading `sys.stdin` waits for terminal input until end-of-file; on the described Unix-style terminal, Ctrl-D signals it. section 11.1.4
A file-processing command can accept either a filename or redirected input. For a `wc`-style utility, make the filename optional and read `sys.stdin` when it is absent. `argparse` can do this with `nargs='?'`, `type=argparse.FileType('r')`, and `default=sys.stdin`; the counting code can then process either source as a file object. section 13.10 section 13.10.2
Redirection can also happen inside Python: assign a file object to `sys.stdout` to capture subsequent output, then restore the previous stream and close the file. For a single `print()` call, its `file` parameter is simpler. In Colaboratory or Jupyter, save a redirection-dependent script as a `.py` file and run it with `! python ...`; restoring a stream there requires saving its previous value rather than relying on `sys.__stdout__`. section 13.6 section 13.10.2
