Chapter 14: Files Flashcards

1
Q

Define a Transient progam.

A

A program that runs for a set period and when it stops, the data disappears.
Running the program again, starts with a clean slate.

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

Define a Persistent program.

A

A program that runs for a long time (or all the time); keep at least some of their data in permanent storage; if they shut down and restart, they pick up where they left off.
Example of persistent program: Windows 10

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

” “ returns a file object that provides methods for working with the file.

A

open

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

The “” method puts data into the file.

A

write

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

You’ve used “open” to read the file, what do you need to do when you are done with the file?

A

close it. variable.close()
Note: If you don’t close the file, it gets closed for you when the program ends.

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

The argument of write has to be a?

A

string
variable.write(str(53))

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

What does ‘%d’ do?

A

Formats an integer to a string.

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

What does ‘%g’ do?

A

Formats to a floating point

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

What does ‘%s’ do?

A

Formats a string

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

Files are organized into?

A

directories

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

The “” module provides functions for working with files and directories.

A

os (stands for operating system)

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

A string like ‘/home/dinsdale’ that identifies a file or directory is called a “”

A

path

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

A simple filename, like memo.txt is also considered a path, but it is a “” path because it relates to the current directory.

A

relative

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

A path that begins with / does not depend on the current directory; it is called an “” path.

A

absolute
»> os.path.abspath(‘memo.txt’)
‘/home/dinsdale/memo.txt’

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

os.path.”” checks whether an item is a file.

A

isfile
os.path.isfile

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

What does os.listdir do?

A

list the files in a given directory.
os.listdir(cwd)
[‘music’, ‘photos’, ‘memo.txt’]

17
Q

os.path.”” takes a directory and a file name and joins them into a complete path.

18
Q

Handling an exception with a try statement is called?

A

catching an exception

19
Q

A “ “ is a sequence of instructions that specifies how to perform a computation.

20
Q

” “ returns a file object that provides methods for working with the file.

21
Q

What will the output of the following program be?
try:
fin = open(‘answer.txt)
fin.write(‘Yes’)
except:
print(‘No’)
print(‘Maybe’)

22
Q

What is the output from the following interactive Python statement?

> > > ‘%g’ % (0.1)

A

0.1
%g is for floating points. If you were to put a %d instead, you would get 0.

23
Q

import os
cwd = os.getcwd()

Which answer is most likely output from the following Python statement?

os.listdir(cwd)

A

[‘Music’, ‘Pictures’, ‘Desktop’, ‘Library’, ‘Documents’, ‘Downloads’]

24
Q

Consider the following Python program.

fin = open(‘words.txt’)
for line in fin:
word = line.strip()
print(word)

What is word?

A

A string with no newline

25
Exceptions allow the programmer to _________________.
Write code to handle runtime errors
26
Assume the following Python code has already executed. import os cwd = os.getcwd() Which answer is most likely output from the following Python statement? os.path.exists(cwd)
True
27
Assume the following Python code has already executed. import os cwd = os.getcwd() Which answer is most likely output from the following Python statement? os.path.isdir(cwd)
True
28
Which of the following Python statements runs without error? a. open('three.txt').write(3) b. open('three.txt','w').write(3) c. open('three.txt','w').write(str(3))
c. The write method needs a string in order to work.
29
import os cwd = os.getcwd() Which answer is most likely output from the following Python statement? os.path.join(cwd, 'Documents/file.txt')
/Users/me/Documents/file.txt
30
Will the following program work? fout = open('text.txt, 'w') line1 = "This is line 1" line2 = 'This is line 2' fout.write(line1, line2)
No. write only takes one argument. You can, however, concatenate the two variables. fout.write(line1 + line2) This is legal.
31
When you are done with a file you should "" the file.
close variable.close()
32
What will the output be for the following print statement? print("format is the number %d method for %s when using floats like %g" % (1,"nerds", 9.9))
format is the number 1 method for nerds when using floats like 9.9
33
What does the following do? os.path.exists('example.txt')
Checks whether a file or directory exists. >>> os.path.exists('example.txt') True
34
What does the following do? os.path.isdir('example.txt')
Checks if it's a directory >>> os.path.isdir('example.txt') False >>> os.path.isdir('/home/dinsdale') True