In Class: Week 7 Monday and Wednesday
Create a week07 subdirectory in your cs21/class directory:
% cd cs21/class
% mkdir week07
Then, from within your week07 subdirectory copy over some python files
from my public directory:
% cd week07
% cp ~newhall/public/cs21/week07/* .
We are going to do some of the following together in class:
strings and lists as objects, and the random library:
- On the class schedule for week6 is a link to information about
using strings and lists as objects, and about using the random library.
Read through this, and look at the sample code that you copied over
from my week07 subdirectoy (stringOps.py, listOps.py, randomOps.py).
In the python interpreter, you can get information about list and str
class method functions by calling help(class_name):
And, if you import the random library, you can get information about
it by calling help too:
>>> import random
- open filetest.py in vim. We will look at this code together and
try running it to see if we understand what it is doing.
Rules for using a file:
You can run help(file) in the python interpreter to see the method functions
associated with a file object.
- open a file (open returns a new file object associated with the
open file). It takes the file name and the mode in which to open the
file as parameters.
infile = open("foo.txt", "r")
- call method functions on the file object to read or write characters
from a file.
line = infile.readline()
- close the file.