Python Pathlib Operations
Path methods do file work on a path. write_text saves text into a file.
Write and read text
write_text saves a string to a file. read_text brings the whole file back as a string.
from pathlib import Path
note = Path("hello.txt")
note.write_text("Hello from a file!", encoding="utf-8")
print(note.read_text(encoding="utf-8"))Make nested folders
parents=True makes every missing folder on the way. exist_ok=True stays calm if the folder is already there.
from pathlib import Path
folder = Path("data") / "2026" / "notes"
folder.mkdir(parents=True, exist_ok=True)
print(folder.is_dir())List a folder
This code fills a small inbox folder first. Then iterdir lists what is inside it.
from pathlib import Path
inbox = Path("inbox")
inbox.mkdir(exist_ok=True)
for name in ["b.txt", "a.txt", "c.md"]:
(inbox / name).write_text(name, encoding="utf-8")
print([p.name for p in sorted(inbox.iterdir())])Match a pattern
The inbox from the last example holds three files. glob with *.txt picks only the text files.
from pathlib import Path
inbox = Path("inbox")
for path in sorted(inbox.glob("*.txt")):
print(path.name)Rename a file
rename moves a file to a new path. The old path stops existing.
from pathlib import Path
old = Path("draft.txt")
old.write_text("draft", encoding="utf-8")
new = Path("final.txt")
old.rename(new)
print(new.exists())
print(old.exists())
print(new.is_file())Example
from pathlib import Path
inbox = Path("note_inbox")
inbox.mkdir(exist_ok=True)
(inbox / "shopping.txt").write_text("eggs and milk", encoding="utf-8")
(inbox / "ideas.txt").write_text("build a birdhouse", encoding="utf-8")
(inbox / "old-name.txt").write_text("rename me", encoding="utf-8")
print("Files:")
for path in sorted(inbox.iterdir()):
print(path.name)
print("Shopping:", (inbox / "shopping.txt").read_text(encoding="utf-8"))
(inbox / "old-name.txt").rename(inbox / "new-name.txt")
print("After rename:")
for path in sorted(inbox.iterdir()):
print(path.name)Key points
- Use write_text to save text into a file
- Use read_text to read a whole file as text
- Use mkdir with parents=True to make missing parent folders too
- Use exist_ok=True to allow a folder that already exists
- Use iterdir to list what sits inside a folder
- Use glob("*.txt") to list only the names that match a pattern
- Use rename to rename a file or move it
- Use exists, is_file and is_dir to test a path
- A Path carries methods that do file work.
In this example, the program makes a folder named note_inbox. It writes three small note files into the folder. It lists the files in sorted order. It reads the shopping note and prints its text. It renames old-name.txt to new-name.txt. The last listing shows the new name in place. Run it in a fresh folder to see this exact output.
Note
iterdir and glob do not promise any order.
Sort the results before you print them.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Python Playground
Exercise
Which call lists only the .txt files inside a folder Path named inbox?