Lecture
12 Data I/O and Files
1.
Read/Write a data file
While
a program is running, its data is in memory. When the program ends, or
the computer shuts down, data in memory disappears. To store data
permanently, you have to put it in a file.
Files are usually stored on a hard drive.
By
reading and writing files, programs can exchange information with each
other and generate printable formats like PDF.
All
of this applies to files as well. To open a file, you specify its name
and indicate whether you want to read or write. Opening a file creates
a file object. In this example, the variable f
refers to the new file object.
The
open function takes two arguments. The first is the name of the file,
and the second is the mode. Mode "w"
means that we are opening the file for
writing.
If
there is no file named test.dat,
it will be created (this
only happens when the mode is 'w'). If there
already is one, it will be replaced by the
file we are writing
In
order to put values into a file, the values must be in the 'string'
format.
The
mode argument is "r"
for reading:
Now
let's write something to the data file:
Please note that if you don't do 'f.close()' in the end, the text won't
be written
into the text file. Which means you must OPEN it to edit and then CLOSE it to complete the process.
Not surprisingly, the read
method reads data from the file. With no
arguments, it reads the entire contents of the file:
Now, let's read
it and print it in the Console:
You must do
'f.close()' every time you change the mode to either 'w' or 'r' in
order to see the data in the text file.
You can only read 5
characters from the file:
The following
function copies a file, reading and writing up to fifty
characters at a time. The first argument is the name of the original
file; the second is the name of the new file:
2.
Writing
variables
3.
Format
operator, %
In Matlab, the
format operator is '\', but in Python, it is '%':
The '%d' type is
formatting the data into a decimal number or an integer.
The
format sequence "%f" formats the next item in the tuple as a
floating-point number, and "%s" formats the next item as a string:
One more
example:
The format
operator enables the variables to be inserted into a string.
For more control
over the format of numbers, we can specify the number of digits as part
of the format sequence:
The number
after
the percent sign is the minimum number of spaces the
number will take up. If the value provided takes fewer digits, leading
spaces are added. If the number of spaces is negative, trailing spaces
are added:
For
example, imagine a dictionary that contains student names as keys and
hourly wages as values. Here is a function that prints the contents of
the dictionary as a formatted report:
By
controlling the width of each value, we guarantee that the columns will
line up, as long as the names contain fewer than twenty-one characters
and the wages are less than one billion dollars an hour.
In order to put
values into a file, you must convert the values to strings. You have
already seen how to do that with str:
You can convert
them into the string type as well:
Or put a list in
to a file:
4.
Exception
Whenever a
runtime error occurs, it creates an exception. Usually, the program
stops and Python prints an error message.
Sometimes
we want to execute an operation that could cause an exception, but we
don’t want the program to stop. We can handle the exception using the
try and except statements. For example, we might prompt the user for
the name of a file and then try to open it. If the file doesn’t exist,
we don’t want the program to crash; we want to handle the exception:
Sometimes
we want to execute an operation that could cause an exception, but we
don’t want the program to stop. We can handle the exception using the
try and except statements.
For example, we
might prompt the user
for the name of a file and then try to open it. If the file doesn’t
exist, we don’t want the program to crash; we want to handle the
exception:
The following
function opened an existing data file
'test.dat', and read the data in it, and print it out. However, if the
data file like 'testie.dat' doesn't exist, the IOError will be handled
by the program and report the response you defined in the program:
'There is no file named testie.dat'.
Tasks:
1.
Convert the data in the following table into a dictionary in Python.
Write the data into an external/independent '.dat' file.
2. Define a function that
carries the dictionary like the one created in Problem 1 and re-align
the data in the data file in the format as follows:
3. Create a
function to copy the 'test.dat' to a new data file 'test_2.dat'. (Do
not mannually copy it over)
4. If the monthly salary is
> 5.5 k, then this employee's income is more than the average. Use
the same data given in Problem 1, create a new
dictionary called 'income' to classify the four employees into two
'keys/categories' - 'overAverage' and 'belowAverage'. Save this new
dictionary into an external data file called 'test_3.dat'.
5. Re-align the data in test_3.dat as follows: