Demonstrate Example of Python Path Support of Division Operator

Demonstrate Example of Python Path Support of Division 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 [2]:
from pathlib import Path
import tempfile
In [5]:
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 [14]:
(child_dir := directory / "subdirectory").mkdir(exist_ok=True)
In [15]:
assert child_dir.exists()

Mind blown.

Here it is in the documentation.

The slash operator helps create child paths