liveBook · Manning

Input/output redirection

As explained in The Quick Python Book, Fourth Edition

Input redirection is a shell mechanism that supplies a command's standard input from a file or another source instead of an interactive terminal.

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

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Explains shell operators and Python stream handling, then develops an optional-filename pattern for a word-count utility.
Examples
Uses a text-replacement script, a word-count utility, and output captured with sys.stdout or print().
What it assumes
Assumes basic Python scripting and file use while introducing how scripts interact with command-line standard streams.
What you can do after
Build it from scratch: write a script that accepts redirected input, writes redirectable output, and optionally accepts a filename.
How it connects
Links standard streams, command-line arguments, pipelines, file objects, and argparse.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.