Please send questions to
st10@humboldt.edu .
FILES...
* a variable only exists when a program is running
* to KEEP data between program runs, a file or a database
is useful...
* and there are several "models"/paradigms for the idea
of a computer file...
BUT here's one: file as a STREAM of CHARACTERS
(each newline is just a newline character (\n),
and there's a special "end of file" (EOF) character)
* file input/output (file i/o) follows a similar approach
in Python and C++:
* you OPEN a file for some purpose
* say for reading, or writing, or appending
(writing: delete the current contents and
write new ones
appending: go to the end of the current
contents and add to the end)
* that open function returns an OBJECT that
you can now use to read, write, or append from/to
that file
Python: file object
C++: stream object
some calls it a file handle...
it's a way to now DO something to your PHYSICAL file;
* NOTE: an OBJECT consists of DATA and MEANS to act
on that data;
or, data and methods;
a METHOD is a function that "belongs" to
an object!
* To call an object's method, it's like
a function call, except you put:
obj.method(arguments)
* you USE the file/handle/stream object methods to
have your way with the file,
* and then you CLOSE the file.
* to open a file in Python:
* use the open function with 2 arguments:
* a string, representing the name of the
physical file involved
(either relative to the open directory
when this is called, OR the whole gory
pathname)
* a string representing the desired action:
"r" for reading, "w" for writing, "a" for appending
* this function returns a "file object"
* this file object can be closed with its close
method:
myFile = open("blah.txt", "r")
myFile.close()
* how can I read from this "file object"?
one of many methods: readline()
myFile.readline() --- returns the next line of
the file, up to and including the newline character
(if any)
What if there are no more lines?
..it just returns an empty string ( "", '')
how can I write?
one of a few methods: write(myString)
...which writes myString to the "current location"
in the file;
* USEFUL NOTE:
Python treats an EMPTY string used where a boolean
is expected like a value of False,
and it treats a NON-EMPTY string used where a boolean
is expected like a value of True
SO:
myReadFile = open("blah.txt", "r")
nextLine = myReadFile.readline()
while (nextLine):
# do what I want with nextLine!
# (I really read something in)
nextLine = myReadFile.readline()
myReadFile.close()
* see showit.py
* for a writing example, see writeSquares.py
...and if you change "w" to "a" in its open
call, you'll see that it adds to the end (appends)
instead of recreating the file if called more than
once with the same file-name
* Now, consider Python LISTS (which are like C++ arrays,
but MORE POWERFUL)
* they're a COLLECTION of ORDERED THINGS that can
be INDIVIDUALLY INDEXED
* in Python, these things can be WHATEVER you want!
(in C++, they ALL have to be the same type...)
* in Python, these can be grown and shrunk at will
(in C++, they have a set size)
* in Python, a list "literal" is any set of
comma-separated things surrounded by square
brackets
[1, 2, 3]
['hi', 'bye', 'why']
[1, 'bye', [1, 2, 3, 4], '']
I can certainly assign a list to a variable:
myList = [1, 'bye', [1, 2, 3, 4], '']
myList is now this whole SET of values!
BUT: I can grab them INDIVIDUALLY by using their
index:
listname[index] is the element with index <index>
in list <listname>
... BUT!!!! the first element has index 0
(think of the index as "how many steps from the
beginning"...)
myList[0]
...is the FIRST element, 1
... and to add to the list,
myList.append("George")
... adds "George" to the end of the list
... to see how many elements are in a list,
len(myList) returns that number of elements
(len works for any composite type in Python ---
like, strings!
len('George Patterson')
* how can I CHANGE an element in a list?
...just SET its indexed value:
myList[2] = "Howdy"
... I've REPLACED whatever was at myList[2] with this
new value.
* (see slice on www.python.org to see how you can
insert stuff in the middle of a list, etc.)