Demonstrate Example of Python Path Support of Slash Operator

Demonstrate Example of Python Path Support of Slash Operator

The pytest documentation says

tmp_path [fixture] is a pathlib/pathlib2.Path object

And then it showed an example of creating a directory with the / slash operator:

d = tmp_path / "sub"
In [1]:
from pathlib import Path
import tempfile
In [2]:
directory = Path(tempfile.mkdtemp())
assert directory.exists()

This is the opportuntity to use an assignment expression.

It captures the Path instance and creates the directory in one statement. The

In [3]:
(child_dir := directory / "subdirectory").mkdir(exist_ok=True)
In [5]:
assert child_dir.exists()
print(child_dir.as_posix())
/tmp/tmphmjqb8_3/subdirectory

Mind blown.

Here it is in the documentation.

The slash operator helps create child paths