In Class: Week 7 Monday and Wednesday
Create a week07 subdirectory in your cs21/class directory:
% cd
% cd cs21/class
% pwd
/home/your_user_name/cs21/class
% mkdir week07
Then, from within your week07 subdirectory copy over some python files
from my public directory:
% cd week07
% pwd
/home/your_user_name/cs21/class/week07
% cp ~newhall/public/cs21/week07/* .
% ls
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):
$ python
>>> help(str)
>>> help(list)
And, if you import the random library, you can get information about
it by calling help too:
$ python
>>> import random
>>> help(random)
File I/O:
- 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:
- 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.
infile.close()
You can run help(file) in the python interpreter to see the method functions
associated with a file object.