Please send questions to
st10@humboldt.edu .
Python 2.5 (r25:51918, Sep 19 2006, 08:49:13)
[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
Type "copyright", "credits" or "license()" for more information.
IDLE 1.2
>>> import sys
>>> sys.path.append("/Users/smtuttle/Humboldt/to-back-up/f06cis180py/180py_lectures/180py_lect07")
>>> # intro to file-handling in Python
>>> # when you open a file a Python...
>>> # ...you open it for some purpose (for reading,
>>> # for writing, for appending, etc.)
>>> # ...and returned is an object you can then use
>>> # to do the desired reading and/or writing
>>> # (that is, you use methods of that object to
>>> # read and write)
>>> # The Python open built-in function expects 2 strings
>>> # as arguments: a string representing the name of
>>> # the file to be opened, and
>>> # a string representing what you want to do to the
>>> # file (the "processing mode")
>>> # (note that the string representing the name of the
>>> # of the file to be opened can be platform-specific
>>> # and absolute or relative;
>>> # if relative, it will be assumed to be relative
>>> # to the directory from which the script is RUN...)
>>> # some example processing modes: 'r' - reading
>>> # 'w' - writing
>>> # 'a' - appending
>>> # what is "relative" is different from IDLE vs. the
>>> # python interpreter; I'm using IDLE in lecture,
>>> # so the following will not work.
>>> # (it *would* work in the python interpreter, IF I had
>>> # called it from the directory where a file
>>> # lyrics.txt resides...)
>>> infile = open("lyrics.txt", "r")
Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
infile = open("lyrics.txt", "r")
IOError: [Errno 2] No such file or directory: 'lyrics.txt'
>>> # for IDLE, then, I need to use the absolute pathname for the
>>> # file in this case. In Windows, this path would look quite
>>> # different...
>>> # NOTE: if you try to run these - CHANGE the paths used in the open
>>> # command to correspond to YOUR files (or call python from the
>>> # directory where those files are/are to be, and just put the
>>> # RELATIVE file name...)
>>> infile = open("/Users/smtuttle/Humboldt/to-back-up/f06cis180py/180py_lectures/180py_lect07/lyrics.txt", 'r')
>>> # what are some file methods? (methods I can use
>>> # with the object returned by open)
>>> #
>>> # (see more from Section 3.9 of the Python Library
>>> # Reference, linked to from public course web page)
>>> #
>>> # read() - read the entire file into a single string
>>> # read(N) - read the next N bytes from the file
>>> # readline() - read next line (through the end-line
>>> # marker)
>>> # readlines() - read the entire file into a list of string
>>> # strings
>>> # (and for <line> in <file> is mentioned in 3.9 as
>>> # recommended for grabbing a line at a time??)
>>>
>>> # close() - closing the file when you are done
>>> # (currently, Python WILL close it when the
>>> # file object is reclained by its garbage collector --
>>> # BUT, this is not part of the Python language
>>> # definition. (i.e., that could change in future)
>>> # BETTER PRACTICE: close what you open!!!
>>>
>>> # write(S) - write the string S to the file
>>> # writeline(L) - write all line strings in list L into
>>> # file
>>>
>>> # so, we have opened "lyrics.txt" for reading, resulting
>>> # in the file object infile
>>>
>>> type(infile)
<type 'file'>
>>> # grab all the lines from lyrics.txt and return them as a single string
>>> content_string = infile.read()
>>> len(content_string)
215
>>> content_string
'Always look on the bright si-ide of life,\n(doo-doo, duh-doo duh-doo duh-doo)\nAlways look on the bright si-ide of life!\n\nSpam de Spam, wonderful Spam!\n\nThe rain in Spain stays mainly in the plain,\nin Spain!\nin Spain!'
>>> infile.close()
>>> infile = open("/Users/smtuttle/Humboldt/to-back-up/f06cis180py/180py_lectures/180py_lect07/lyrics.txt", "r")
>>> # read the first character/byte
>>> infile.read(1)
'A'
>>> # now let's read the next 3 characters
>>> infile.read(3)
'lwa'
>>> # note that the input methods generally return strings --
>>> # but, remember, you can always convert digits in a string
>>> # to the corresponding number...
>>> int('3')
3
>>> float('3.1')
3.1000000000000001
>>> # will this read the rest of the current line? yes:
>>> infile.readline()
'ys look on the bright si-ide of life,\n'
>>> # this reads the next line...
>>> infile.readline()
'(doo-doo, duh-doo duh-doo duh-doo)\n'
>>> # will this read the rest of the lines into a list? yes:
>>> what = infile.readlines()
>>> len(what)
7
>>> what
['Always look on the bright si-ide of life!\n', '\n', 'Spam de Spam, wonderful Spam!\n', '\n', 'The rain in Spain stays mainly in the plain,\n', 'in Spain!\n', 'in Spain!']
>>> for line in what:
print line
Always look on the bright si-ide of life!
Spam de Spam, wonderful Spam!
The rain in Spain stays mainly in the plain,
in Spain!
in Spain!
>>> # (note that the line read with readline, readlines
>>> # includes the newline character... thus the "excess"
>>> # of newlines above, since print gave me a newline,
>>> # too...!)
>>> # if you call readlines and you are at the end of file -
>>> # you get an empty list.
>>> now_what = infile.readlines()
>>> now_what
[]
>>> infile.close()
>>> infile = open("/Users/smtuttle/Humboldt/to-back-up/f06cis180py/180py_lectures/180py_lect07/lyrics.txt", "r")
>>> # looping through a file, reading a line at a time, is quite reasonable;
>>> # this is but one example;
>>> nextline = infile.readline()
>>> while (nextline != ''):
print nextline,
nextline = infile.readline()
Always look on the bright si-ide of life,
(doo-doo, duh-doo duh-doo duh-doo)
Always look on the bright si-ide of life!
Spam de Spam, wonderful Spam!
The rain in Spain stays mainly in the plain,
in Spain!
in Spain!
>>> infile.close()
>>> ends_file = open("/Users/smtuttle/Humboldt/to-back-up/f06cis180py/180py_lectures/180py_lect07/ends_w_newline.txt", 'r')
>>> nextline = ends_file.readline()
>>> while (nextline != ''):
print nextline
nextline = ends_file.readline()
I
end
with
a
newline.
>>> # (above: what a difference a comma makes in a print command...!)
>>> ends_file.close()
>>> ends_file = open("/Users/smtuttle/Humboldt/to-back-up/f06cis180py/180py_lectures/180py_lect07/ends_w_newline.txt", 'r')
>>> nextline = ends_file.readline()
>>> while (nextline != ''):
print nextline,
nextline = ends_file.readline()
I
end
with
a
newline.
>>> ends_file.close()
>>> infile = open("/Users/smtuttle/Humboldt/to-back-up/f06cis180py/180py_lectures/180py_lect07/lyrics.txt", 'r')
>>> infile.readline()
'Always look on the bright si-ide of life,\n'
>>> infile.readline()
'(doo-doo, duh-doo duh-doo duh-doo)\n'
>>> infile.readline()
'Always look on the bright si-ide of life!\n'
>>> infile.readline()
'\n'
>>> infile.readline()
'Spam de Spam, wonderful Spam!\n'
>>> infile.readline()
'\n'
>>> infile.readline()
'The rain in Spain stays mainly in the plain,\n'
>>> infile.readline()
'in Spain!\n'
>>> infile.readline()
'in Spain!'
>>> infile.readline()
''
>>> # above: note that the last line read from this file doesn't end with
>>> # \n --- lyrics.txt doesn't happen to end with a newline character;
>>> # (and if you call readline() past the end of file - you get an empty
>>> # string...)
>>> ends_file = open("/Users/smtuttle/Humboldt/to-back-up/f06cis180py/180py_lectures/180py_lect07/ends_w_newline.txt", 'r')
>>> infile.close()
>>> ends_file.readline()
'I\n'
>>> ends_file.readline()
'end\n'
>>> ends_file.readline()
'with\n'
>>> ends_file.readline()
'a \n'
>>> ends_file.readline()
'newline.\n'
>>> ends_file.readline()
''
>>> # ends_w_newline.txt did happen to end with a newline - so we see
>>> # \n at the end of its last line
>>> ends_file.close()
>>> # note that playing with show_guts.py (posted along with this) shows that
>>> # where the script is RUNNING is where it looks for
>>> # a file - not where the script "lives"
>>> # (show_guts.py now has a comment explaining/reminding you how to
>>> # run a python script from the UNIX/Linux command-line)
>>> # now: some writing practice
>>> crude_copy = open("/Users/smtuttle/Humboldt/to-back-up/f06cis180py/180py_lectures/180py_lect07/lyrics_copy.txt", 'w')
>>> input = open("/Users/smtuttle/Humboldt/to-back-up/f06cis180py/180py_lectures/180py_lect07/lyrics.txt", 'r')
>>> # reading all the lines from input's corresponding file to a list -
>>> # writing that list of lines into crude_copy's corresponding file
>>> crude_copy.writelines( input.readlines() )
>>> # now, look at what files you have in the current directory ---
>>> # cs-server> ls
>>> # ... you'll see a file lyrics_copy.txt!
>>> #
>>> # look at the contents of lyrics_copy.txt and lyrics.txt ---
>>> # cs-server> more lyrics_copy.txt
>>> # cs-server> more lyrics.txt
>>> # ...and they sure look similar!
>>> #
>>> # UNIX/Linux diff command shows differences between two files -
>>> # shows NOTHING if file contents are identical.
>>> # cs-server> diff lyrics.txt lyrics_copy.txt
>>> # ... shows these files' contents are indeed identical.
>>> crude_copy.close()
>>> input.close()
>>> # more writing practice...
>>> write_some_more = open("/Users/smtuttle/Humboldt/to-back-up/f06cis180py/180py_lectures/180py_lect07/stuff.txt", "w")
>>> write_some_more.write("Hello, how are you?")
>>> write_some_more.write("Oh, I am fine.")
>>> write_some_more.close()
>>> # now look at stuff.txt (cs-server> more stuff.txt) ---
>>> # note that write(S) writes exactly the string you
>>> # give - no newline unless you put one in...
>>>
>>> # also note: if you open an EXISTING file for
>>> # writing - the existing contents are NUKED!
>>> outfile = open("/Users/smtuttle/Humboldt/to-back-up/f06cis180py/180py_lectures/180py_lect07/stuff.txt", 'w')
>>> outfile.write("Bonjour\n")
>>> outfile.close()
>>> # now look at stuff.txt --- only Bonjour is there!
>>> # appending lets you add to the end of a file without
>>> # nuking its current contents
>>> outfile = open("/Users/smtuttle/Humboldt/to-back-up/f06cis180py/180py_lectures/180py_lect07/stuff.txt", 'a')
>>> outfile.write("Comment allez-vous?\n")
>>> outfile.write("Au revoir\n")
>>> outfile.close()
>>> # now you see Bonjour as well as Comment allez-vous and Au revoir
>>> # non-file bonus: interactive input in Python...
>>> # (slight interlude)
>>> # function raw_input takes a string as argument -
>>> # the "prompt" to be printed to the screen -
>>> # and it captures whatever the user types
>>> # (up to the next Enter/return) and returns
>>> # it as a string
>>> looky = raw_input("Type something, please: ")
Type something, please: 145
>>> looky
'145'
>>> looky = raw_input("Type something else: ")
Type something else: Hi How are YOU???
>>> looky
'Hi How are YOU???'
>>> # there is also a function called input();
>>> # input tries to collect what is typed
>>> # and return it as a number.
>>> # BUT: BEWARE!!!
>>> # in grabbing the input, Python EXECUTES it --
>>> # a malicious user could type in a PYTHON COMMAND
>>> # along with a number, and that command would
>>> # be executed --- might delete a file, or who
>>> # knows what?!
>>>
>>> # so: safer to stick with raw_input and just convert
>>> # the string returned to the desired numeric type...
>>>
>>> # so: a few examples to close...
>>> # first: pig_it.py, an interactive-input example
>>> # (posted along with this session)
>>>
>>> import pig_it
Please type string to pig-latinize: yes, I can do this
Your pig-latinized string is:
es-yay i-ay an-cay o-day his-tay
Thank you, and please come again.
>>> # lect07.py - contains function pig_latin_file (and is posted
>>> # along with this session)
>>> import lect07
>>> lect07.pig_latin_file("/Users/smtuttle/Humboldt/to-back-up/f06cis180py/180py_lectures/180py_lect07/lyrics.txt", "/Users/smtuttle/Humboldt/to-back-up/f06cis180py/180py_lectures/180py_lect07/piggy_lyrics.txt")
>>> # now look and see what is inside piggy_lyrics.txt!
>>> # lect07.py also contains a function letter_freq_file...
>>> lect07.letter_freq_file("/Users/smtuttle/Humboldt/to-back-up/f06cis180py/180py_lectures/180py_lect07/lyrics.txt")
a - 14 : XXXXXXXXXXXXXX
b - 2 : XX
d - 12 : XXXXXXXXXXXX
e - 10 : XXXXXXXXXX
f - 5 : XXXXX
g - 2 : XX
h - 9 : XXXXXXXXX
i - 18 : XXXXXXXXXXXXXXXXXX
k - 2 : XX
l - 9 : XXXXXXXXX
m - 4 : XXXX
n - 13 : XXXXXXXXXXXXX
o - 19 : XXXXXXXXXXXXXXXXXXX
p - 7 : XXXXXXX
r - 4 : XXXX
s - 12 : XXXXXXXXXXXX
t - 7 : XXXXXXX
u - 4 : XXXX
w - 3 : XXX
y - 4 : XXXX
>>> lect07.letter_freq_file("/Users/smtuttle/Humboldt/to-back-up/f06cis180py/180py_lectures/180py_lect07/lect07.py")
a - 17 : XXXXXXXXXXXXXXXXX
b - 2 : XX
c - 5 : XXXXX
d - 9 : XXXXXXXXX
e - 67 : XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
f - 20 : XXXXXXXXXXXXXXXXXXXX
g - 11 : XXXXXXXXXXX
h - 11 : XXXXXXXXXXX
i - 60 : XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
l - 34 : XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
m - 11 : XXXXXXXXXXX
n - 55 : XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
o - 21 : XXXXXXXXXXXXXXXXXXXXX
p - 31 : XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
q - 2 : XX
r - 23 : XXXXXXXXXXXXXXXXXXXXXXX
s - 13 : XXXXXXXXXXXXX
t - 54 : XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
u - 26 : XXXXXXXXXXXXXXXXXXXXXXXXXX
v - 1 : X
w - 10 : XXXXXXXXXX
x - 6 : XXXXXX
y - 3 : XXX
z - 1 : X
>>>