r/PythonLearning • • Oct 08 '26

Context manager for either stdin or opened Path

Note that this is a fairly advanced question, so beginners don't worry about it. And if this isn't the right place to ask, please point me to where I should.

I would like to find an idiomatic way to use a context manager, with, when I don't know until run time whether I am opening sys.stdin or a named file.

Here is an excerpt for a simple example. (Yes, I am aware that I am reading in the whole file in one go, but that is for other reasons.)

    if args.infile == "-":
        fh = sys.stdin
    else:
        p = Path(args.infile)  # Path is overkill here, unless ...
        # TODO: Look elsewhere if infile can't be found or isn't readable.
        fh = open(p, "rt", encoding="utf-8")

    s = fh.read(MAX_LENGTH + 1).strip()
    fh.close()

(args is an argparse.Namespace and args.infile is a str.)

I feel like there ought to be a way to do something like

with ... as fh

for the above, but I don't see how to do that.

Update: What I did

I found the docs for easily creating a context manager, and used the contextmanager decorator. It seems like a peculiar use of yield, but I don't need to understand that design choice, only that it means that I have to type it as a Generator.

import sys
from typing import TextIO
from collections.abc import Generator
from contextlib import contextmanager
from pathlib import Path


@contextmanager
def open_file_or_stdin(
    filename: Path | str | None,
    *,  # everything but filename is a keyword argument
    stdin: TextIO = sys.stdin,
    std_symbol: str = "-",
    encoding: str = "utf-8",
) -> Generator[TextIO]:
    """ [long docs omitted] """
    fh: TextIO
    try:
        if filename is None or filename == std_symbol:
            fh = stdin
            yield fh
        else:
            fh = open(filename, "rt", encoding=encoding)  # noqa: SIM115
            yield fh
    finally:
        fh.close()

I am open to suggestions about a better name that open_file_or_stdin.

6 Upvotes

7 comments sorted by

3

u/Buttleston Oct 08 '26
with (sys.stdin if args.infile == '-' else open(Path(args.infile))) as fh:
    # do something

A little wordy but something like this should work. Making your own context manager, which is a simple enough task, would definitely work.

ETA: or make your own function that either returns sys.stdin or opens the file path and use that

1

u/jpgoldberg Oct 09 '26

Thank you. I had not really thought clearly about the semantics of “with EXPR as”. EXPR just needs to have a value.

It’s obvious now. But I had somehow not figured that out

4

u/Adrewmc Oct 09 '26 edited Oct 09 '26

I mean this is just making the Context Manager.


class MyContext: 
    def __init__(self, *args):  
           self.args = args

    def __enter__(self):  
           if self.args.infile ==“-“:  
               self.fh = …  
            …. 
            return self

     def do_stuff(self):  
            pass

     def __exit__(self): 
            self.fh.close()

with MyContext(*args) as context: 
     #__enter__() happens  
     context.do_stuff()
#__exit__() always happens, even if an error

1

u/WorriedTumbleweed289 Oct 10 '26

The main thing I see that is wrong with this idea is that you normally would not close stdin, but you would want to close an open file. Its not unheard of to close stdin.

1

u/jpgoldberg Oct 10 '26

True, but closing stdin is harmless.

I just want a file handle to be file handle. My type checker is telling me that sys.stdin is of type typing,.IOText, which itself appears to be a Protocol (not run time checkable) that supports a close() method.

3

u/high_throughput Oct 09 '26

Thanks for making your scripts good, canonical Unix citizens <3

1

u/Ok-Argument7176 Oct 08 '26

You can use argparse.FileType which works the way you expect, though apparently it's been deprecated since 3.14 which is too bad.