Files
0. Learning objectives
- Read and write files in
Python
1. Files
This is the way Wikipedia defines a file: "A computer file is a computer resource for recording data in a computer storage device, primarily identified by its file name. Just as words can be written to paper, so can data be written to a computer file."
Whether we are reading or writing a file, we need a way to open files. This is done with a built-in
function in Python, called the open function. The open function takes
in two parameters (both are strings):
-
filename: the name of the file we would like to open. You can either specify the full path (i.e. the
full location on your computer of where this file is located), otherwise the path will be assumed to be
relative to where your current
Pythonscript lives. An example of a full path might be/Users/mwazowsk/Documents/cs145/homeworks/homework7/herecomes.txton OS X, orC:\mwazowsk\Documents\cs145\homeworks\homework7\herecomes.txton Windows. Assuming we are writing aPythonscript and running it from the same "homework7" folder, then a relative path would simply beherecomes.txt(this is somewhat more common). -
mode: specifies how we will interact with the file. In other words, are we reading contents from a
file or are we writing to it? Furthermore, how should we read data from the file? There are two
options for the "how": format and access type.
For format, we either read data in text format (by specifying a't'in the mode string) or in binary format (by specifying a'b'in the mode string). We will only work with the text format in this course. You can leave out the't'because 'text' is the default mode.
For access type, you can specify a'r'for reading and a'w'for writing to the file.
For example, to read a file called 'lyrics.txt' you would use
open('lyrics.txt', 'r'). To write a file called 'frequency.txt', you would use
open('frequency.txt', 'w').
Now, what does the open function returns to us? It returns a File object, specifically a
_io.TextWrapper object (a built-in type). Don't worry too much about this: just think of it as a
file object to iterate on. We need this file object to either read from, or write to the file depending on how
we opened it.
A file object is iterable, just like strings, lists, dictionaries and range's are
iterable. Remember, in the case of strings, the iteration variable iterates over characters; with lists, the
iteration variable iterates over items; with dictionaries, the iteration variable iterates over the keys; with
range the iterable variable iterates over the numbers generated by range. With a
file object, the iteration variable iterates over the lines in the file. So typing
for L in file: will iterate through all lines in file. On each iteration,
L receives the next line in the file as a string.
Have a look at the example below in which:
- we open the file
lyrics.txtin read-text more - we iterate through the lines of the tile and store the lyrics of the entire "Here Comes The Sun" song in
the
lyricsvariable - we use some useful string methods (functions) and properties (variables) from the
stringpackage to clean out and manipulate our text (I encourage to you look at thehelp()for each of the methods to find out how they operate and look at the value of the properties (variables) from thestringpackage to see what they contain) - we create a dictionary that evaluate the frequency of each word (remember the example from the data structure chapter?)
- we finally write the word frequency data, as comma separated values to a new file
called
concordance.txt
When you're done with your file, you should always close it!
This can be done with the command file.close() (assuming file is the the name of
the variable you used to store the file object you received from the open function).
To write contents to a file, we can use the write() function defined for file objects. This is
very similar to the print() function we have been using to print information to the console.
However, there are a few differences. First, we can only pass one parameter (a string) to the
write function (in contrast to how you can pass in multiple things to print()
separated by commas). This means you will need to create a single string (via concatenation) to write a line
to a file. Second, whereas print() implicitly creates a new line after printing, you need to
specify that you want a new line when using write(). Remember the '\n'
(newline) characters? You can use these to create a new line in the file you are writing to.