In Class: Week 8, list algorithms
Create a week08 subdirectory
in your cs21/class directory and copy over my starting point files:
$ cd
$ cd cs21/class
$ pwd
/home/your_user_name/cs21/class
$ mkdir week08
$ cd week08
$ pwd
/home/your_user_name/cs21/class/week08
$ cp ~turnbull/public/cs21/week08/* .
$ ls
fileTest.py foo.txt listAlgs.py
- 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. We will likely only use readline and
readlines to read in data from a file.
- Searching for Information
After taking a look at what list_algs.py does, we are going to add some
code to list_algs.py.
- First, we are going to add a function to find an element in a
list of integer values.
- Next, we are going to think about a better way to implement this
function.