Free Online Flashcard Deck

6 File Input and Output Free Online FlashCards

Study 6 File Input and Output with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.

12 cards
01
Front

Why use `with open(...)` for file operations?

Back

The `with` statement automatically closes the file when the indented block ends, even if an exception occurs.

02
Front

What do the file modes `"r"`, `"w"`, and `"a"` do?

Back

`"r"` reads an existing file, `"w"` creates or replaces a file, and `"a"` appends to its end.

03
Front

What does a file object's `read()` method return?

Back

`read()` returns the remaining file contents as one string.

04
Front

How should a large text file usually be processed?

Back

Iterating over the file object processes one line at a time and is generally the most memory-efficient pattern for large text files.

05
Front

What does `write()` do, and what does it return?

Back

`write()` writes one string and returns the number of characters written; it does not add a newline automatically.

06
Front

When should a file be opened in binary mode?

Back

Use binary mode such as `"rb"` or `"wb"` for bytes and non-text data such as images, audio, and compressed files.

07
Front

How do relative and absolute file paths differ?

Back

A relative path is interpreted from the program's current working directory; an absolute path specifies the complete location.

08
Front

Why use `pathlib.Path` for file paths?

Back

`pathlib.Path` constructs and inspects paths with platform-independent operations, avoiding manual path-separator handling.

09
Front

Why explicitly specify UTF-8 for text files?

Back

Specify `encoding="utf-8"` so characters are decoded consistently and behavior is less dependent on the computer's locale.

10
Front

Why use `newline=""` when opening a CSV file?

Back

Open CSV files with `newline=""` so Python's `csv` module can handle newline conventions correctly.

11
Front

What do `Path.write_text()` and `Path.read_text()` do?

Back

`write_text()` replaces an existing file with text, while `read_text()` opens, reads, and returns the decoded contents.

12
Front

Which file-reading exceptions commonly need targeted handling?

Back

Handle `FileNotFoundError` when the file is missing, `PermissionError` when access is denied, and `UnicodeDecodeError` when text is not valid UTF-8.