Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions python-pathlib/README.md
Original file line number Diff line number Diff line change
@@ -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')
```
6 changes: 6 additions & 0 deletions python-pathlib/display_dir_tree.py
Original file line number Diff line number Diff line change
@@ -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}")
3 changes: 3 additions & 0 deletions python-pathlib/hello.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from pathlib import Path

print(f"You can find me here: {Path(__file__).parent}!")
5 changes: 5 additions & 0 deletions python-pathlib/read_shopping_list.py
Original file line number Diff line number Diff line change
@@ -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))
12 changes: 12 additions & 0 deletions python-pathlib/shopping_list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Shopping List

## Fruit

* Banana
* Apple
* Peach

## Candy

* Chocolate
* Nougat Bits
7 changes: 7 additions & 0 deletions python-pathlib/unique_path.py
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions python-pathlib/write_plain_shoppinglist.py
Original file line number Diff line number Diff line change
@@ -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")
Loading