I/O Flashcards

1
Q

What does a program do when it needs to save data for future use?

A

writes a data in a file. Normally used .txt files so far.

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

Name some software that store data

A
Word processors
Image Editors
Spreadsheet
Games
Web browsers
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Programmers usually refer to storing data to a file as?

A

writing data to

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

How is a piece of data written to a file?

A

copied from a variable in RAM to the file

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

output file

A

used to describe a file that the data is written to

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

Why is it called an output file?

A

because the program stores output in it

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

What is the process of retrieving data from a file known as?

A

reading

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

Input file

A

used to describe a file that is read from

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

Why is it called an input file?

A

because the program gets the input from it

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

What are the three steps that must be taken when a file is used by a program.

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
11
Q

Open the file

A

Opening a file creates connection between the file and the program.
Opening an output file creates a file on the disk and allows the program to write data to it.
Opening an input file allows to read the data from a file

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

Process a file

A

In this step data is either written to the file or read from the file

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

close a file

A

When the program is finished using the file, it must be closed. Closing a file disconnects the file from a program.

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

What are the two types of files?

A

Binary and text

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

A text file?

A

encoded as text using scheme : ASCII or unicode.

Viewable with text editors

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

A binary file

A

not converted to text. Cannot be opened with text editors

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

What are the two file access methods?

A

sequential

direct

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

What is Sequential access file?

A

You access data from the beginning of the file to the end of the file. You cant jump around. Old tape. Cannot hop around. If you want to listen to track #5 have to listen to all tracks 1 -4

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

What is Direct access file?

A

You can jump directly to any piece of data in the file without having to read the data that comes before it.
LIke MP3 players

20
Q

Direct access file is also known as?

A

Random access file

21
Q

What is a file object?

A

An object that is associated with a specific file and provides a way for the program to work with the file.

22
Q

What function do you use to open a file in Phyton?

A

open

23
Q

Syntax of writing to a file

A

FileWrite = open (“filename.txt”, someMode)

24
Q

Name the modes

A

r, w and a

25
Q

Describe the r mode

A

read only. The file cannot be changed or written to.

26
Q

Describe the w mode

A

open the file for write only. If file exists, delete the contents. If file does not exist, re-create it.

27
Q

Describe ‘a’ mode

A

Open a file to be written to. If a file exits, add to the end. If not creates it.

28
Q

test_file = open (r ‘c:\Blake\here.txt’, ‘w’)

What is r? Why is it needed?

A

raw string.

Otherwise \ will be considered as escape character and give out error.

29
Q

Syntax to write something to a file

A

FileWrite.write(string)

30
Q

Syntax to close

A

FileWrite.close()

31
Q

The data that is stored in a file is frequently organized in?

A

records and fields

32
Q

What is a record?

A

A complete set of data about an item

33
Q

What is a field?

A

An individual piece of data within a record.

34
Q

What is an exception?

A

An exception is an error that occurs when the program is running causing the program to abruptly halt.

35
Q

How can you handle exceptions?

A

use try/catch statements

36
Q

What is ‘traceback”?

A

It is an error message that gives information regarding the line numbers that caused exception.

37
Q

try:
(tab)statement
(tab)statement
what is this statement block called?

A

the try suite.

38
Q

What is created when an exception is thrown in the memory?

A

an exception object

39
Q

How do you display the default error message

A

except ValueError as sucks:

print(sucks)

40
Q
the else clause in the try/catch statement.
try:
   statement
except someerror:
   statement
else:
   statement
what is the block of statement after else called?
explain how this works.
A

else suite.
try some thing gets executed. If an exception is thrown, goes to the except and something is done. Does not go to else statement…
BUT if not exception is raised, skips the except and goes to else.

41
Q

Explain how finally clause in try/execute statement works.

A
finally at the end.
try:
statement
except:
statement
finally:
statement.
Now finally executes after try/except and whether or not the exceptions are raised or not raised.
42
Q

What is the purpose of finally?

A

For clean up operation. It closes files and other resources

43
Q

What if an exception is not handled?

A

It will cause the program to halt.

44
Q

What type of exception does a program raise when it tries to open an non-existence file?

A

IO error

45
Q

What type of exception does a program raise when float tries to convert to non-numeric string to number?

A

Value Error

46
Q

How do you handle multiple exceptions?

A

add as many except: statements