Chapter 6 Flashcards

(32 cards)

1
Q

Why must programs use files?

A

To retain data between program runs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is writing data to a file?

A

Saving data on a file (output file)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is reading data from a file?

A

Retrieving data from a file (input file)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the three steps when a program uses a file?

A

Open the file, process the file, close the file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the two main types of files?

A

Text files and binary files

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the two ways to access file data?

A

Sequential access and direct access

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a filename extension?

A

A short sequence of characters at the end of a filename indicating file type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does Python’s open() function do?

A

Opens a file, creates a file object, and associates it with a disk file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the common file modes in Python?

A

‘r’ for reading, ‘w’ for writing, ‘a’ for appending

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What happens when you open a file with ‘w’ mode?

A

If it exists, it is overwritten; if not, it is created

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What happens when you open a file with ‘a’ mode?

A

Data is written to the end of the file without erasing existing content

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does write() do?

A

Writes a string to a file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does read() do?

A

Reads the entire file into a string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does readline() do?

A

Reads one line from a file, including the \n

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does rstrip() do when reading files?

A

Strips trailing characters (like \n) from a string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Why must numbers be converted before writing to a file?

A

They must be converted to strings using str()

17
Q

Why must numbers be converted after reading from a file?

A

They are read as strings and must be converted with int() or float()

18
Q

How does Python detect the end of a file using readline()?

A

An empty string ‘’ is returned

19
Q

How do you use a for loop to read files?

A

for line in file_object:
statements

20
Q

What does the with statement do when opening files?

A

Opens and automatically closes files when the block ends

21
Q

What is a record?

A

A set of data that describes one item

22
Q

What is a field?

A

A single piece of data within a record

23
Q

What is an exception in Python?

A

An error that occurs during program execution

24
Q

What is a traceback?

A

An error message showing the type of exception and line number

25
What is an exception handler?
Code that responds when exceptions are raised to prevent program crashes
26
What is the general format of try/except?
try: statements except ExceptionName: statements
27
What happens if no exception occurs in a try block?
The except block is skipped
28
What is multiple exception handling?
Writing multiple except clauses for different exception types
29
How can you display an exception’s default error message?
Assign the exception object to a variable and print it (except ValueError as err: print(err))
30
What does the else clause do in exception handling?
Executes only if no exceptions were raised
31
What does the finally clause do?
Executes regardless of whether an exception occurred; used for cleanup
32
What happens if an exception is not handled?
The program halts with an error message