Chapter 7 (files and exceptions)
Pickles
How do you open a file?
textFile = open('file.txt', 'r')
- Here the file.txt, is the file you want to open
- r represents the access mode
- This will return all the contents of the file in this case
What are all the access modes?
'r'
read mode
'w'
write mode
'a'
append mode
'r+'
read from and write to a text file
'w+'
write to and read from a text file
'a+'
append and read from a text file
How do you close a file?
fileName.close()
How do i read from a file?
textFile = open('textFile.txt', 'r')
textFile.read(1)
- The code above will read the first character
Note: The read method will only read from where i left off, if i was to say repeat the second line of code it would return the second character. To start from the beginning you should close and reopen the text file.
How do you read a line from a file?
textFile.readlines(3)
- This returns a list of the first 3 lines
Can you loop through a text file?
yes, it will iterate through each line
for lines in textFile:
print(lines)
How to write to a text file?
textFile = open('textFile.txt', 'r')
#note that this creates a new blank file and will replace any old ones
textFile.write('wowza\n')
#note the use of the \n to create a new line, calling the function alone
#wont do this
text.write('yo')
#this string will be appended to the end of the text file
Can you write a list to a text file?
yes, each element will be placed onto the same line, so must have a \n for a new line
What does the pickle module do?
This converts python objects into a byte stream and back again
Shelves (shelfs)
What does the shelve module do?
This allows you to retrieve objects and store them from and to the byte stream
What . profix is at the end of a bit stream file?
.dat
What do you do to the access modes so that they can work with binary files?
add a 'b' at the end
rb
wb
ab
rb+
wb+
ab+
How do i store data in a .dat file?
import shelve, pickle
#imports the nessisary modulas for the code to run
listName = [1,2,3]
f = open('fileName.dat', 'wb')
#note the b for binary
pickle.dump(listName, f)
#.dump takes two inputs the object you want to store and the file name
How do i load data from a .dat file?
import shelve, pickle
f = open('fileName.dat', 'rb')
variable = pickle.load('fileName.dat')Note: we cannot pick which object is loaded, each object will come out one at a time first stored to last stored
How to store data with a shelf
import shelve, pickle
f = shelve.open('fileName.dat')
f['this is a key'] = ['data', 'moreDataaa']How do i make sure that all the shelfs data is written to the file?
use
fileName.sync()
or when you close it
fileName.close()
How do i retrieve data from a selfed file?
fileName['key name']
Try Except
What is the syntax for try and except
try:
code
except TypeError:
code
except (NameError, IOError):
code
else:
code
#note the else keyword can only be used after a except keyword blockGive an exsample of the as keyword
try:
code
except ValueError as e:
print(e)
#prints ValueError
/Untitled.png)
/Untitled%201.png)