Free Online Flashcard Deck

7 File Input and Output Free Online FlashCards

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

12 cards
01
Front

What is the general form of Python’s open() function?

Back

The general form is open(file, mode="r", encoding=None). It returns a file object for reading or writing according to the selected mode.

02
Front

What does file mode "r" do?

Back

Mode "r" reads an existing file and is the default mode for open().

03
Front

What does file mode "w" do?

Back

Mode "w" writes to a file, replacing its existing contents. It creates the file if necessary.

04
Front

What does file mode "a" do?

Back

Mode "a" writes at the end of an existing file and preserves its contents. It creates the file if necessary.

05
Front

When should mode "x" be used?

Back

Mode "x" creates a new file and fails if a file with that name already exists.

06
Front

What does adding "b" to a file mode mean?

Back

Appending "b" selects binary mode, such as "rb" or "wb". Text modes handle str, while binary modes handle bytes.

07
Front

Why explicitly specify UTF-8 when opening text files?

Back

Specify encoding="utf-8" for predictable text processing across computers. Without it, Python may use a platform-dependent default.

08
Front

How does file.read() behave?

Back

read() returns the remaining file contents as one string. A size argument limits how much it reads, and later calls continue from the current position.

09
Front

Why iterate over a file instead of calling read()?

Back

Iterating over a file processes it one line at a time without loading the entire file into memory, making it suitable for large text files.

10
Front

What does file.write() return, and what does it omit?

Back

write() writes a string and returns the number of characters written. It does not add a newline automatically, so include "\n" when needed.

11
Front

Why use / with pathlib.Path objects?

Back

The / operator joins path components using the operating system’s appropriate conventions, making pathlib paths portable.

12
Front

Why use a with statement for files?

Back

A with statement closes the file automatically when execution leaves the block, even if an exception occurs. This safely releases the resource and flushes buffered output.