Python
Files & I/O
30 flashcards · answers and spaced-repetition review in the KnowCard app
Why prefer with open(...) as f: over a manual open() / f.close() pair?
What does open("out.txt", "w") do to an existing file before you write anything?
Name the core single-letter file modes for open() and what each does when the file already exists.
You open with "r" and the file does not exist. What happens — and how does that differ from "w"?
Difference between text mode ("t", default) and binary mode ("b") — what type comes back from read()?
Why can specifying encoding= matter, and what does Python use if you omit it in text mode?
After data = f.read(), what does a second f.read() return on the same file object?
Iterating a file (for line in f:) vs f.readlines() vs f.read() — which to use for a huge file and why?
Predict the output:
```
with open("d.txt") as f:
for line in f:
print([line])
(file has the line: Spain Spanien)
```
When writing text with f.write(...), what does write NOT do for you that print does?
What does f.tell() return, and what is the safe rule for seek() in text mode?
In binary mode, what do the three whence values of seek(offset, whence) mean?
Predict: you open a file with mode "a" and call f.seek(0) then f.write("X"). Where does "X" land?
What does "r+" give you that "w+" does not, and why does the distinction matter?
What does read(size) count — characters or bytes — and how does mode change the answer?
Why might data you wrote with f.write(...) not be on disk yet, and what forces it out?
After a file object is closed (end of with block or f.close()), what happens if you call f.read()?
What does f.writelines([...]) do that might surprise you compared to its name?
You read a file once with for line in f:, then loop for line in f: again in the same with block. What does the second loop do?
Which file-object attributes let you read back how the file was opened?
Why can you f.seek() around a file but not rewind stdin or a network connection?
A file has a byte that is invalid in your chosen encoding. How does open()'s errors parameter change what happens on read?
How does f.readline() signal end-of-file, and how does that let you drive it with iter(f.readline, "")?
Beyond gzip, which standard-library modules cover bzip2, .xz files, ZIP, and TAR archives?
A function insists on writing to an open file object, but you just want the result as a string in memory. What do you pass it?
You never opened stdin or stdout, yet print() and input() clearly do I/O. What are they actually operating on?
Besides filename, mode, and encoding, what two lesser-used parameters does open() accept, and what does each control?
You need to hand an open file to a low-level OS call that wants a file descriptor. Which method gives you that, and what descriptors do stdin/stdout have?
You've written a shorter record over the start of an existing file but stale bytes remain at the end. Which method removes them, and where does it cut?
You want to read and write a .gz file without manually invoking zlib. What does the standard library give you, and how is compression strength set?
Start learning today
Free to start — download the app or use it in your browser.
