Practice: pathlib Module

« Read the chapter

Q1. Join with slash

BeginnerCoding

Build the path docs/guide/intro.md using Path and the / operator, then print it as text with forward slashes (use as_posix).

Goal: Join paths.

Q2. Print stem and suffix

BeginnerCoding

For the path archive/photo.jpeg print the stem and the suffix on one line separated by a space.

Goal: Read path parts.

Q3. Write and read

BeginnerCoding

Write the text Python into hello.txt using pathlib and print the text read back.

Goal: Read and write text.

Q4. Make nested folders

BeginnerCoding

Create the folders out/2024/jan with one call and print whether the last one is a directory.

Goal: Create folders.

Q5. Count text files

IntermediateCoding

Inside folder w create a.txt, b.txt and c.md. Print how many .txt files glob finds in w.

Goal: Search with glob.

Q6. Change the extension

IntermediateCoding

Write the text data into d.txt, create the path with suffix .csv using with_suffix and print its name.

Goal: Change a suffix.

Q7. Recursive search

IntermediateCoding

Create t/x.py and t/sub/y.py. Print the sorted names of all .py files found by rglob in t.

Goal: Search recursively.

Q8. Read lines

IntermediateCoding

Write three lines a, b and c into letters.txt, then print how many lines read_text().splitlines() gives.

Goal: Read lines.

Q9. Path parts

BeginnerOutput

What does this print?

Code
from pathlib import PurePosixPath
p = PurePosixPath("/home/amy/notes.txt")
print(p.name)
print(p.parent)

Goal: Read path parts.

Q10. Parts tuple

BeginnerOutput

What does this print?

Code
from pathlib import PurePosixPath
print(PurePosixPath("a/b/c.txt").parts)

Goal: Use parts.

Q11. Double suffix

IntermediateOutput

What does this print?

Code
from pathlib import PurePosixPath
p = PurePosixPath("backup.tar.gz")
print(p.suffix, p.stem)

Goal: Understand suffix.

Q12. With name

IntermediateOutput

What does this print?

Code
from pathlib import PurePosixPath
p = PurePosixPath("logs/app.log")
print(p.with_name("error.log"))

Goal: Replace a name.

Q13. Exists check

IntermediateOutput

What does this print?

Code
from pathlib import Path
p = Path("ghost.txt")
print(p.exists())
p.write_text("boo")
print(p.exists())

Goal: Path versus file.

Q14. Fix the join

BeginnerDebugging

This code raises TypeError. Fix it so it prints data/file.txt.

Code
from pathlib import Path
p = "data" / "file.txt"
print(p.as_posix())

Goal: Fix operator use.

Q15. Fix the suffix

IntermediateDebugging

This code raises AttributeError. Fix it so it prints notes.md.

Code
from pathlib import PurePosixPath
p = PurePosixPath("notes.txt")
p.suffix = ".md"
print(p)

Goal: Paths are immutable.

Q16. Fix the missing parents

IntermediateDebugging

This code raises FileNotFoundError. Fix it so it prints True.

Code
from pathlib import Path
p = Path("x/y/z")
p.mkdir()
print(p.is_dir())

Goal: Create parent folders.

Q17. os.path versus pathlib

BeginnerConceptual

Give two reasons why pathlib is often easier to use than os.path.

Goal: Compare modules.

Q18. Path versus file

BeginnerConceptual

Does Path("report.txt") create a file on disk? Explain.

Goal: Know what Path does.

Q19. Rename all text files

AdvancedApplication

Create folder w with a.txt and b.txt. Rename every .txt file to .bak and print the sorted names in w.

Goal: Batch rename.

Q20. Total size of a folder

AdvancedApplication

Create folder s with files one (3 characters) and two (5 characters). Print the total size in bytes using stat().st_size.

Goal: Sum file sizes.