Programming with Python - Files

0.0(0)
Studied by 0 people
call kaiCall Kai
Locked
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/30

flashcard set

Earn XP

Description and Tags

Chapter 12 - Files

Last updated 4:06 PM on 8/17/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

31 Terms

1
New cards
How to open a file for reading in Python

Use open("filename.txt","r"); raises FileNotFoundError if missing.

2
New cards
What the read() method does
Reads entire file as one string.
3
New cards
What readline() does
Reads one line at a time.
4
New cards
What readlines() returns
List of all lines in the file.
5
New cards
How iterating over a file works
Reads one line per loop, very memory-efficient.
6
New cards
Why newline characters appear

Lines include "\n"; remove with rstrip().

7
New cards
How Python behaves at EOF
read() returns "" and iteration stops.
8
New cards
Why files must be closed
Releases system resources and prevents data issues.
9
New cards
How to open a file for writing
Use open("file.txt","w") to overwrite or "a" to append.
10
New cards
How to write text to a file

Use write(); convert numbers to strings.

11
New cards
Why file output is buffered

Writes may be delayed; flush() forces writing.

12
New cards
Why closing a file after writing matters
Ensures all buffered data is saved.
13
New cards
What os.stat() is used for
Gets file metadata like size and timestamps.
14
New cards
How to delete a file
Use os.remove("filename.txt").
15
New cards
Why os.path.join() is recommended
Creates correct paths on any OS.
16
New cards
Useful os.path functions
exists(), isfile(), isdir(), getsize(), split(), sep.
17
New cards
What os.walk() does
Traverses directories and lists folders/files.
18
New cards
Difference between binary and text files

Text stores characters; binary stores raw bytes.

19
New cards
How to read a binary file
Open with "rb" and use read().
20
New cards
What a bytes object represents
Immutable sequence of byte values 0–255.
21
New cards
Ways to create bytes objects
From strings, lists, literals, or hex escapes.
22
New cards
Purpose of the struct module
Packs/unpacks binary data with format codes.
23
New cards
What endianness means
">" = big-endian, "<" = little-endian.
24
New cards
Why the with statement is used
Automatically closes files even on errors.
25
New cards
How to read a file with with
Use with open(...) as f: to ensure safe closing.
26
New cards
Why with is preferred
Avoids leaks and makes code cleaner.
27
New cards
What a CSV file is
Common table-like format using commas.
28
New cards
How to read a CSV file

Use csv.reader; rows come as lists of strings.

29
New cards
Why CSV values need type conversion

Reader returns strings; convert manually.

30
New cards
How to read CSV with custom delimiter

Use csv.reader(..., delimiter=";").

31
New cards
How to write CSV files

Use csv.writer with writerow() or writerows(); newline="" on Windows.