pathlib

Path, read_text, write_text, and glob — prefer this over os.path.

pathlib.Path is the modern way to work with files. Prefer it over os.path.join string soup.

Goal

Write a note, read it back, and list *.txt files with glob.

read_text and write_text

from pathlib import Path

note = Path("shift.txt")
note.write_text("Nairobi kiosk open\n", encoding="utf-8")
print(note.read_text(encoding="utf-8"))
print("name:", note.name, "suffix:", note.suffix)

Always pass encoding="utf-8" so the file is not platform-dependent.

/ operator

from pathlib import Path

folder = Path("/uploads")
path = folder / "shift.txt"
print(path)
print(path.parent)

/ on a Path joins. It does not divide numbers.

glob

from pathlib import Path

Path("nairobi.txt").write_text("Nairobi\n", encoding="utf-8")
Path("mombasa.txt").write_text("Mombasa\n", encoding="utf-8")
names = sorted(p.name for p in Path(".").glob("*.txt"))
print(names)
Pitfall

open("file") without encoding uses a default you do not control. Path.read_text(encoding="utf-8") is explicit.