API Reference
Update or create files atomically
with_tempfile.write_text(fpath, contents, *, encoding=None)
Atomically write the specified text to the specified file.
Source code in python/src/with_tempfile/impl.py
with_tempfile.append_text(fpath, contents, *, encoding=None)
If the file exists, atomically append text, otherwise write the new contents.
Source code in python/src/with_tempfile/impl.py
Path manipulation helpers
with_tempfile.pathutil
Helpers for manipulating pathlib.Path
objects and strings.
This module's main purpose is to define the PathLike
type that
may be used for function arguments that may be of any type suitable
for passing to the system Python file handling routines.
It is somewhat similar to the os.PathLike
class, but it has
a different set of allowed classes, e.g. it allows strings.
PathLike = pathlib.Path | str
module-attribute
Something that may be used as a path.
as_path(path)
encoding_tuple(encoding)
Split the first of the requested encoding names from the rest.
Source code in python/src/with_tempfile/pathutil.py
read_text(path, *, encoding)
Read a file's contents, try to decode it using the specified encodings in order.
The return value is a tuple containing the full text read from the file and the name of the encoding used to decode it.
The encoding
parameter may be either None, a string, or an iterable of strings.
In the latter case the specified encodings are tried in order; the first one that
successfully decodes the file's contents is returned.
Source code in python/src/with_tempfile/pathutil.py
Temporary file handling
with_tempfile.temputil
Helpers that combine temporary files and pathlib routines.
This module defines the NamedTemporaryTextFile
function and
the TemporaryDirectory
class that parallel the corresponding
classes from the system tempfile
module, but also have
the additional path
and resolved_path
fields.
The goal is to make it easier to use pathlib.Path
objects
everywhere, while still using the system temporary file routines.
TemporaryDirectory
Bases: TempDir
A temporary directory with a ready-made pathlib.Path
path.
Note that the __enter__()
method (and consequently with TemporaryDirectory(...) as tempd:
)
returns a pathlib.Path
object instead of a string!
Source code in python/src/with_tempfile/temputil.py
path
instance-attribute
The path to the tempfile as a pathlib.Path
object.
resolved_path
instance-attribute
The resolved path to the tempfile as a pathlib.Path
object.
__enter__()
Enter a context, return the path to the directory.
Note that this method returns a pathlib.Path
object instead of a string!
__init__(suffix=None, prefix=None, dir=None, ignore_cleanup_errors=False)
Create the temporary directory, initialize our new fields.
Source code in python/src/with_tempfile/temputil.py
TemporaryTextFile
Bases: TempTextWrap
A named temporary file with a ready-made pathlib.Path
path.
Source code in python/src/with_tempfile/temputil.py
path
instance-attribute
The path to the tempfile as a pathlib.Path
object.
resolved_path
instance-attribute
The resolved path to the tempfile as a pathlib.Path
object.
__init__(file, name, delete=True)
Create the temporary file, initialize our new fields.
Source code in python/src/with_tempfile/temputil.py
__repr__()
Provide a Python-esque representation.
Source code in python/src/with_tempfile/temputil.py
unset_delete(*, also_on_close=True)
Unset the "delete on drop" and also possibly the "delete on close" flag.
The caller took care of the file, e.g. renamed it, removed it, or passed it to another consumer to use as-is.
Source code in python/src/with_tempfile/temputil.py
NamedTemporaryTextFile(*, buffering=-1, dir=None, encoding=None, prefix=None, suffix=None, delete=True)
Create a temporary file that holds its path.