File Input and Output Flashcards
7 cards from real Python practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 7 File Input and Output flashcards as text
What does file.seek(0) accomplish?
Answer: Moves the file pointer to the beginning
file.seek(0) moves the read/write position back to byte offset 0, which is the start of the file.
What is the third argument to file.seek(offset, whence)?
Answer: The reference position: 0=start, 1=current, 2=end
The whence argument specifies the reference point: 0 for start of file, 1 for current position, and 2 for end of file.
Which module provides the NamedTemporaryFile context manager?
Answer: tempfile
The tempfile module provides NamedTemporaryFile, TemporaryFile, and other utilities for creating temporary files.
What does file.truncate(n) do?
Answer: Resizes the file to at most n bytes
file.truncate(size) resizes the file to at most size bytes; if size is omitted, the current position is used.
In Python, which exception is raised when a file is not found?
Answer: FileNotFoundError
FileNotFoundError is raised when a file or directory is requested but cannot be found; it is a subclass of OSError.
What does the with statement guarantee when used with open()?
Answer: The file is closed automatically when the block exits
The with statement invokes the file object's __exit__ method, which closes the file even if an exception is raised.
What does csv.DictReader return for each row?
Answer: An OrderedDict (or dict in 3.8+) mapping header names to values
csv.DictReader yields each row as a dict where keys come from the first-row headers and values are the row's fields.