Lies den Code sorgfältig
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmpdir:
base = Path(tmpdir)
f = base / "data.txt"
f.write_text("hello\nworld\n")
# 1. round-trip read
print(f.read_text().strip())
# 2. path decomposition
print(f.name)
print(f.stem)
print(f.suffix)
# 3. derived path via .with_suffix
new = f.with_suffix(".bak")
print(new.name)
# 4. glob
(base / "a.py").write_text("")
(base / "b.py").write_text("")
print(sorted(p.name for p in base.glob("*.py")))
# 5. exists predicate
print(f.exists(), (base / "ghost.txt").exists())
Was wird das Programm ausgeben? Schreib hier: