Chapter 7 - Files and Exceptions

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/37

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 3:57 PM on 7/30/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

38 Terms

1
New cards

text file

contains data that has been encoded as text

2
New cards

binary file

contains data that has not been converted to text

3
New cards

open()

opens a file and returns it as a file object

4
New cards

file.close()

closes the file, after which no more reads or writes to the file are allowed

5
New cards

file.read()

returns the file contents as a string

6
New cards

file.readlines()

returns a list of strings, where the first element is the contents of the first line, the second is the contents of the second line, and so on

7
New cards

file.readline()

returns a single line at a time

8
New cards

file.write()

writes a string argument to a file

9
New cards

file.seek(offset)

change file’s current position

10
New cards

file.write(‘text’)

to write a string to the file

11
New cards

mode

indicates how a file is opened

12
New cards

“r”

open the file for reading

13
New cards

“w”

open the file for writing

  • if the file doesn’t exist, then the file is created

  • contents of an existing file are overwritten

14
New cards

“a”

open the file for appending

  • if the file doesn’t exist, then the file is created

  • writes are added to the end of existing file contents

15
New cards

What does the “+” modifier mean for modes?

it allows you to perform both reading and writing on the same file stream

16
New cards

“r+”

open for reading and writing without truncating the file; the pointer is at the beginning

17
New cards

“w+”

truncates the file to zero length or creates a new one for reading and writing

18
New cards

“a+”

open for. reading and writing, appends at the end, creates the file if it doesn’t exist

19
New cards

CSV (comma-separated values) file

a text file that contains data separated by a specific character

20
New cards

csv module

can be used to help read and write files in the csv format

21
New cards

output buffer

output to a file is buffered by the interpreter before being written to the computer’s hard disk, so there may be a delay between a call of write() and writing that data to the disk

22
New cards

file.flush()

manually forces the program to clear its internal memory buffer and write all pending data down to the operating system’s file buffer

23
New cards

exception

error that occurs while a program’s running, causing the program to abruptly halt

24
New cards

traceback

a lengthy error message that is shown in the sample run and gives information regarding the line number(s) that caused the exception

25
New cards

exception handling

process of responding to unexpected or unwanted events and errors during execution

26
New cards

exception handling format

  • try:

  • except:

  • else:

  • finally:

27
New cards

try:

wraps code that might cause an exception

28
New cards

except:

handles the specific exception raised

29
New cards

else:

executes if no exception occurs

30
New cards

finally:

executes regardless of exceptions; often used for cleanup

31
New cards
32
New cards

EOFError

input() hits an end-of-file condition (EOF) without reading any input

33
New cards

KeyError

a dictionary key is not found in the set of keys

34
New cards

ZeroDivisionError

divide by zero error

35
New cards

ValueError

invalid value (e.g. input mismatch)

36
New cards

IndexError

index is out of bounds

37
New cards

custom exception type

must define a new class that inherits from the built-in Exception class

38
New cards

custom exception type syntax

class MyCustomError(Exception):
     """Exception raised for specific application errors."""
     pass