Practice: pathlib Module
Q1. Join with slash
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
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
Write the text Python into hello.txt using pathlib and print the text read back.
Goal: Read and write text.
Q4. Make nested folders
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
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
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
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
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
What does this print?
from pathlib import PurePosixPath
p = PurePosixPath("/home/amy/notes.txt")
print(p.name)
print(p.parent)Goal: Read path parts.
Q10. Parts tuple
What does this print?
from pathlib import PurePosixPath
print(PurePosixPath("a/b/c.txt").parts)Goal: Use parts.
Q11. Double suffix
What does this print?
from pathlib import PurePosixPath
p = PurePosixPath("backup.tar.gz")
print(p.suffix, p.stem)Goal: Understand suffix.
Q12. With name
What does this print?
from pathlib import PurePosixPath
p = PurePosixPath("logs/app.log")
print(p.with_name("error.log"))Goal: Replace a name.
Q13. Exists check
What does this print?
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
This code raises TypeError. Fix it so it prints data/file.txt.
from pathlib import Path
p = "data" / "file.txt"
print(p.as_posix())Goal: Fix operator use.
Q15. Fix the suffix
This code raises AttributeError. Fix it so it prints notes.md.
from pathlib import PurePosixPath
p = PurePosixPath("notes.txt")
p.suffix = ".md"
print(p)Goal: Paths are immutable.
Q16. Fix the missing parents
This code raises FileNotFoundError. Fix it so it prints True.
from pathlib import Path
p = Path("x/y/z")
p.mkdir()
print(p.is_dir())Goal: Create parent folders.
Q17. os.path versus pathlib
Give two reasons why pathlib is often easier to use than os.path.
Goal: Compare modules.
Q18. Path versus file
Does Path("report.txt") create a file on disk? Explain.
Goal: Know what Path does.
Q19. Rename all text files
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
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.