From 0ea41a21be00e4df8c64ed1060ebf2adb195514d Mon Sep 17 00:00:00 2001 From: Real Python Bot <42617967+realpython-bot@users.noreply.github.com> Date: Wed, 21 Oct 2026 15:30:41 +0000 Subject: [PATCH] Add python-pathlib materials for Python's pathlib Module: Taming the File System --- python-pathlib/README.md | 51 ++++++++++++++++++++++ python-pathlib/display_dir_tree.py | 6 +++ python-pathlib/hello.py | 3 ++ python-pathlib/read_shopping_list.py | 5 +++ python-pathlib/shopping_list.md | 12 +++++ python-pathlib/unique_path.py | 7 +++ python-pathlib/write_plain_shoppinglist.py | 6 +++ 7 files changed, 90 insertions(+) create mode 100644 python-pathlib/README.md create mode 100644 python-pathlib/display_dir_tree.py create mode 100644 python-pathlib/hello.py create mode 100644 python-pathlib/read_shopping_list.py create mode 100644 python-pathlib/shopping_list.md create mode 100644 python-pathlib/unique_path.py create mode 100644 python-pathlib/write_plain_shoppinglist.py diff --git a/python-pathlib/README.md b/python-pathlib/README.md new file mode 100644 index 0000000000..03322b30b3 --- /dev/null +++ b/python-pathlib/README.md @@ -0,0 +1,51 @@ +# Python's pathlib Module: Taming the File System + +This folder provides the code examples for the Real Python tutorial [Python's pathlib Module: Taming the File System](https://realpython.com/python-pathlib/). + +## Setup + +You need Python 3.9 or later. The `pathlib` module is part of the standard library, so there's nothing to install. + +Run the scripts from inside this folder, because several of them read and write files relative to the current working directory. + +## Usage + +Print the folder that contains the script: + +```console +$ python hello.py +You can find me here: /path/to/python-pathlib! +``` + +Read the groceries from `shopping_list.md`: + +```console +$ python read_shopping_list.py +* Banana +* Apple +* Peach +* Chocolate +* Nougat Bits +``` + +Write the groceries to a new file, `plain_list.md`: + +```console +$ python write_plain_shoppinglist.py +``` + +The `display_dir_tree.py` and `unique_path.py` modules define functions that you can import in a REPL session: + +```pycon +>>> from pathlib import Path +>>> from display_dir_tree import tree +>>> tree(Path.cwd()) ++ /path/to/python-pathlib + + README.md + + display_dir_tree.py + ... + +>>> from unique_path import unique_path +>>> unique_path(Path.cwd(), "test{:03d}.txt") +PosixPath('/path/to/python-pathlib/test001.txt') +``` diff --git a/python-pathlib/display_dir_tree.py b/python-pathlib/display_dir_tree.py new file mode 100644 index 0000000000..86fa8c8754 --- /dev/null +++ b/python-pathlib/display_dir_tree.py @@ -0,0 +1,6 @@ +def tree(directory): + print(f"+ {directory}") + for path in sorted(directory.rglob("*")): + depth = len(path.relative_to(directory).parts) + spacer = " " * depth + print(f"{spacer}+ {path.name}") diff --git a/python-pathlib/hello.py b/python-pathlib/hello.py new file mode 100644 index 0000000000..b216c16475 --- /dev/null +++ b/python-pathlib/hello.py @@ -0,0 +1,3 @@ +from pathlib import Path + +print(f"You can find me here: {Path(__file__).parent}!") diff --git a/python-pathlib/read_shopping_list.py b/python-pathlib/read_shopping_list.py new file mode 100644 index 0000000000..03cc860f26 --- /dev/null +++ b/python-pathlib/read_shopping_list.py @@ -0,0 +1,5 @@ +from pathlib import Path + +content = Path("shopping_list.md").read_text(encoding="utf-8") +groceries = [line for line in content.splitlines() if line.startswith("*")] +print("\n".join(groceries)) diff --git a/python-pathlib/shopping_list.md b/python-pathlib/shopping_list.md new file mode 100644 index 0000000000..2eef868cc8 --- /dev/null +++ b/python-pathlib/shopping_list.md @@ -0,0 +1,12 @@ +# Shopping List + +## Fruit + +* Banana +* Apple +* Peach + +## Candy + +* Chocolate +* Nougat Bits diff --git a/python-pathlib/unique_path.py b/python-pathlib/unique_path.py new file mode 100644 index 0000000000..7ee43bbd5f --- /dev/null +++ b/python-pathlib/unique_path.py @@ -0,0 +1,7 @@ +def unique_path(directory, name_pattern): + counter = 0 + while True: + counter += 1 + path = directory / name_pattern.format(counter) + if not path.exists(): + return path diff --git a/python-pathlib/write_plain_shoppinglist.py b/python-pathlib/write_plain_shoppinglist.py new file mode 100644 index 0000000000..38c7f02c3a --- /dev/null +++ b/python-pathlib/write_plain_shoppinglist.py @@ -0,0 +1,6 @@ +from pathlib import Path + +content = Path("shopping_list.md").read_text(encoding="utf-8") +groceries = [line for line in content.splitlines() if line.startswith("*")] + +Path("plain_list.md").write_text("\n".join(groceries), encoding="utf-8")