Chapter 6 - Filenames and exceptions Flashcards

1
Q

Define Persistence

A

Programs that run for a long time, or all the time.
Some data is in the permanent storage(hard drive)
A shutdown/restart sequence has them picking up where the program ended.

Examples: operating systems, web servers, apps on phone

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

Define Transient

A

These programs run for a short time and produce some output but when they end the data disappears. If you run the program again it starts with a clean slate.

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

Output File

A

A file that data is written to it

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

Input file

A

a file from which data is read

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

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

A

Open file, process file, close file

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

What are the two general types of files?

A

Text file
Binary File

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

What is a text file?

A

contains data that has been encoded as text

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

What is a binary file?

A

Contains data that has not been converted to text. (faster for computer because it is already in binary)

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

What are the two ways to access data stored in a file:

A

sequential access, direct access

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

Define Sequential access

A

File read sequentially from beginning to end, can’t skip ahead (cassette tape)

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

Define Direct access

A

can jump directly to any piece of data in a file ( a CD)

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

What are filename extensions?

A

short sequences of characters that appear at the end of a filename preceded by a period.

Extension indicates type of data stored in the file.

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

What is a file object?

A

object associated with a specific file

provides a way for a program to work with the file. file object referenced BY A VARIABLE.

File objects allow us to use,access, and. manipulate all the user accessible files.

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

What does the open function do?

A

used to open a file.
creates a file object and associates it with a file on the disc.

Format:
file_object = open(‘filename’,’mode’)

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

What is a mode? what are the three types that you can use when opening a file?

A

A mode is a string specifiying how the file will be opened.

‘r’ = read
‘w’ = write
‘a’ = append

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

What is a method?

A

A function that belongs to an object.

performs operations using that object.

example: file objects write method used to write data to a file
format: file_variable.write(string)

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

What is the read method?

A

file object method that reads entire file contents into memory.
Only works if the file was opened for reading.
The contents are returned as a string

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

What is the readline method?

A

file object method that reads a line from the file

Line returned as a string, including ‘\n’

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

Define read position:

A

marks the location of the next item to be read from a file

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

What is the rstrip method?

A

string method that strips specific characters from the end of the string

21
Q

What happens if you open the file in ‘w’ mode if the file already exists

A

the file will be overwritten

22
Q

How do you write numbers to a file

A

You must first convert them to a string

23
Q

What does the str function do:

A

converts a value to a string

numbers are read from a text file as strings.
Must be converted to numeric type in order to perform mathematical calculations.

Use int and float functions to convert strings to numeric values.

24
Q

Which method uses an empty string as a sentinel when end of file is reached?

A

readline method.

while line != ‘ ‘ #does not equal empty

25
Q

Python allows the programmer to write a for loop that automatically reads lines in a file and stops when end of the file is reached.

Format:
for lin in file_object:
statements

How many times will the loop iterate over each line in the file?

A

Once

26
Q

Define record:

A

a set of data that describes one item

27
Q

describe field:

A

a single piece of data within a record

28
Q

How do you write a record to a sequential access file?

A

By writing the fields one after the other

29
Q

How do you read a record from a sequential access file?

A

By reading each field until record complete

30
Q

When working with records, it is important to be able to do what (5) things?

A

1.) Add records
2.) display records
3.) search for a specific record
4.) modify records
5.) delete records

31
Q

Define a directory: (folders)

A

they organize files within memory. Each time a program runs it has a current directory which is the default directory for most operations.

32
Q

What does the os(operating system) library provide?

A

functions for working with files and directories.

33
Q

what does the following code snippet do:

import os
cwd = os.getcwd()
cwd

A

returns the name of the current working directory

34
Q

define a path:

A

it identifies a file or directory where a file is stored in memory.

Example c:/mydocuments/python/practice.py

35
Q

os.path.isfile does what?

A

checks whether it is a file

36
Q

os.path.join does what

A

takes a directory and file name and joins into a complete path

37
Q

os.walk does what?

A

generates the file names in a directory tree by walking the tree either top-down or bottom-up. For each directory in the tree rooted at directory top(including top itself), it yields a 3-tuple (dirpath, dirnames, filenames)

38
Q

define exception:

A

error that occurs while program is running, usually causes a program to abruptly halt

39
Q

define trace back:

A

error message that gives information regarding line numbers that caused the exception. indicates the type of exception and brief description of the error that caused the exception to be raised.

40
Q

How can many exceptions be prevented

A

careful coding
ex: input validation

some exceptions cannot be avoided by careful coding
ex: trying to convert non-numeric string to an integer
ex: trying to open for reading a file that doesn’t exist

41
Q

What is an exception handler?

A

code that represents when exceptions are raised and prevents program from crashing.

in python written as try/except statement

try:
statements
except exceptionName:
statements

42
Q

What is a try suite?

A

statements that can potentially raise an exception

43
Q

What is a handler:

A

statements contained in an except block

44
Q

exception object:

A

object created in memory when an exception is thrown.

usually contains default error message pertaining to the exception

can assign the exception object to a variable in an except clause.

can pass exception object to variable to print function to display the default error message.

45
Q

Try/ Except statement may include and optional else clause which:

A

which appears after all the except clauses.

Is aligned with try and except clauses
syntax similar to else clause in decision structure.

46
Q

What is an else suite:

A

block of statements executed after statements in try suite only if no exceptions were raised.

If exception was raised, the else suite is skipped.

47
Q

Try/except statements may include an optional finally clause:

A

which appears after all the except clauses

aligned with try and except clauses.

format:
finally:
statements

48
Q

what is the finally suite

A

block of statements after the finally clause.

execute whether an exception occurs or not

purpose is to perform cleanup before exiting

49
Q

What are the two ways an exception can go unhandled?

A

1.) no except clause specifying exception of the right type

2.) exception raised outside a try suite

In both cases exception will cause program to halt.