Python
Files & Paths (pathlib)
40 flashcards · answers and spaced-repetition review in the KnowCard app
What does os.path.splitext("archive.tar.gz") return?
After shutil.move("data", "backup/data"), the source is gone but you expected a copy. What does move actually do?
What is the difference between p.read_text() and open(p).read(), and when does each leave a file handle open?
A function does if Path(name).is_file(): process(name). The file exists but the branch never runs. What's the most likely cause?
What does os.path.basename("/usr/include/") return — and why might it surprise you?
What does this return, and what's the trap?
```
import os.path
os.path.join("/home/me", "/etc", "x")
```
os.path.exists(path) returns False for a file you can see in the file manager. What are two reasons besides a typo?
What does glob.glob("*.txt") return for a folder with files but also hidden .config?
What's the difference between a path being absolute vs relative, and what do Path.cwd() and Path.home() give you?
You ran files = Path(".").glob("*.log") then looped over files twice; the second loop is empty. Why?
You have a base directory and a filename to combine into one path. Why is base + "/" + name wrong, and what should you write instead?
For p = Path("/home/me/report.tar.gz"), what are p.name, p.stem, and p.suffix?
What does Path("a/b/c.txt").parts give you, and how is it different from .parent?
You want every .py file anywhere under a project, including subfolders. What does Path("src").glob("*.py") miss, and what fixes it?
What happens here if `out` already exists?
```
from pathlib import Path
Path("out").mkdir()
Path("out").mkdir()
```
You call Path("a/b/c").mkdir(exist_ok=True) but a and b don't exist yet. What happens?
Both shutil.copy(src, dst) and shutil.copyfile(src, dst) copy a file. What can copy do that copyfile cannot?
Why is shutil.rmtree(path) considered dangerous, and how is it different from os.rmdir?
When does Path.iterdir() raise, and what does it actually yield?
After os.chmod("a_file", 0o640), who can do what to the file?
What exactly does os.listdir(path) return — full paths or names? And what's always excluded?
os.mkdir("a/b/c") fails when a and b don't exist. What error, and which function fixes it — and what do both do if the target already exists?
Relative paths in the os module are resolved against the current working directory. Which two os functions read and change it?
os.rename(src, dst) is called when a file already exists at dst. What happens, and how do os.renames and os.replace differ?
What does this print on any OS?
```
from pathlib import Path
p = Path("logs") / "app" / "out.txt"
print(p)
```
shutil.copytree(src, dst) fails if dst already exists. What does it do, and how do you skip certain files?
shutil.make_archive("test", "zip", "data") — what's the gotcha with the first argument, and what does it return?
What does tempfile.TemporaryDirectory() create and return?
What does os.access("python.exe", os.F_OK | os.X_OK) actually check, and when does it return True?
You pass a directory path to os.remove(). What happens?
os.removedirs("a/b/c") — which directories does it try to delete, and in what order?
os.walk(top) yields what for each folder, and what does topdown=False change?
Two things that can silently go wrong with os.walk: how are errors and symbolic links handled?
Which os.path functions give a file's size in bytes, its modification time, and a symlink-free version of a path?
os.path.commonprefix([...]) — why might its result not be a usable path?
What does os.path.split("/home/username/") return, and what's the trap with a trailing separator?
os.path.normcase and os.path.splitdrive behave differently across platforms. What does each do?
Beyond copying, which shutil helpers locate an executable and report free disk space?
tempfile.TemporaryFile() is opened in which mode, and why does tmp.write("hello") raise a TypeError?
When would you reach for tempfile.NamedTemporaryFile instead of tempfile.TemporaryFile?
Start learning today
Free to start — download the app or use it in your browser.
