Search code examples
pythonpathlib

How can I represent stdin and stderr with pathlib.Path?


I love the pathlib.Path api and use it a lot for quick cli tools. Especially with typer. I have a few tightly related questions:

  • In UNIX cli commands - is the de facto standard for stdin. Is that the same under Windows?
  • Is there a clean, cross-platform way to have a pathlib.Path object (or actually the {POSIX,Windows}Path it automatically becomes) to represent stdin?
  • And what about stdout?

Solution

  • There is no cross-platform pseudo file name for standard input and standard output.

    In POSIX, it's /dev/stdin for standard input and /dev/stdout for standard output, although many CLI tools would accept - in the command line arguments as a shorthand for standard input or standard output.

    In Windows, it's CONIN$ for standard input and CONOUT$ for standard output, or simply CON for both standard input and standard output.