open

open(key: str, *subkeys: str, name: str, mode: Literal['r', 'rt', 'w', 'wt'] = 'r', open_kwargs: Mapping[str, Any] | None = None) Generator[StringIO, None, None][source]
open(key: str, *subkeys: str, name: str, mode: Literal['rb', 'wb'], open_kwargs: Mapping[str, Any] | None = None) Generator[BytesIO, None, None]

Open a file.

Parameters:
  • key – The name of the module. No funny characters. The envvar <key>_HOME where key is uppercased is checked first before using the default home directory.

  • subkeys – A sequence of additional strings to join. If none are given, returns the directory for this module.

  • name – The name of the file to open

  • mode – The read or write mode, passed to open()

  • open_kwargs – Additional keyword arguments passed to open()

  • ensure_exists – Should the directory the file is in be made? Set to true on write operations.

Yields:

An open file object

This function should be called inside a context manager like in the following

import pystow

with pystow.open("test", name="test.tsv", mode="w") as file:
    print("Test text!", file=file)