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 the 'a+' mode do when opening a file in Python?
Answer: Opens for reading and writing, pointer at end, creates file if missing
The 'a+' mode opens a file for both reading and appending; the pointer is at the end and the file is created if it does not exist.
Which method reads all lines of a file into a list?
Answer: file.readlines()
file.readlines() returns a list where each element is a line from the file including the newline character.
What happens if you call open() with mode 'x' on a file that already exists?
Answer: It raises a FileExistsError
Mode 'x' is exclusive creation; it raises FileExistsError if the file already exists.
What does the os.path.getsize() function return?
Answer: File size in bytes
os.path.getsize(path) returns the size of the file at the given path in bytes.
Which built-in function is used to rename a file in Python?
Answer: os.rename()
os.rename(src, dst) renames the file or directory from src to dst.
What is the default encoding used by open() on most modern Python installations?
Answer: The locale/platform default (often utf-8)
If encoding is not specified, open() uses the locale-preferred encoding, which is utf-8 on most modern systems.
Which statement about binary mode ('rb') is TRUE?
Answer: Data is returned as bytes objects
In binary mode, read operations return bytes objects instead of str, with no newline translation.