Chapter 14: Files Flashcards
Define a Transient progam.
A program that runs for a set period and when it stops, the data disappears.
Running the program again, starts with a clean slate.
Define a Persistent program.
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
” “ returns a file object that provides methods for working with the file.
open
The “” method puts data into the file.
write
You’ve used “open” to read the file, what do you need to do when you are done with the file?
close it. variable.close()
Note: If you don’t close the file, it gets closed for you when the program ends.
The argument of write has to be a?
string
variable.write(str(53))
What does ‘%d’ do?
Formats an integer to a string.
What does ‘%g’ do?
Formats to a floating point
What does ‘%s’ do?
Formats a string
Files are organized into?
directories
The “” module provides functions for working with files and directories.
os (stands for operating system)
A string like ‘/home/dinsdale’ that identifies a file or directory is called a “”
path
A simple filename, like memo.txt is also considered a path, but it is a “” path because it relates to the current directory.
relative
A path that begins with / does not depend on the current directory; it is called an “” path.
absolute
»> os.path.abspath(‘memo.txt’)
‘/home/dinsdale/memo.txt’
os.path.”” checks whether an item is a file.
isfile
os.path.isfile
What does os.listdir do?
list the files in a given directory.
os.listdir(cwd)
[‘music’, ‘photos’, ‘memo.txt’]
os.path.”” takes a directory and a file name and joins them into a complete path.
.join
Handling an exception with a try statement is called?
catching an exception
A “ “ is a sequence of instructions that specifies how to perform a computation.
program
” “ returns a file object that provides methods for working with the file.
open
What will the output of the following program be?
try:
fin = open(‘answer.txt)
fin.write(‘Yes’)
except:
print(‘No’)
print(‘Maybe’)
No
Maybe
What is the output from the following interactive Python statement?
> > > ‘%g’ % (0.1)
0.1
%g is for floating points. If you were to put a %d instead, you would get 0.
import os
cwd = os.getcwd()
Which answer is most likely output from the following Python statement?
os.listdir(cwd)
[‘Music’, ‘Pictures’, ‘Desktop’, ‘Library’, ‘Documents’, ‘Downloads’]
Consider the following Python program.
fin = open(‘words.txt’)
for line in fin:
word = line.strip()
print(word)
What is word?
A string with no newline
Exceptions allow the programmer to _________________.
Write code to handle runtime errors
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
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
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.
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
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.
When you are done with a file you should “” the file.
close
variable.close()
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
What does the following do?
os.path.exists(‘example.txt’)
Checks whether a file or directory exists.
»> os.path.exists(‘example.txt’)
True
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