File Processing
You have been reading and writing to the standard input and output until now. What if you want to read an external file and write to a different file? We will see how to use actual data files in this lab. Python provides basic functions and methods necessary to manipulate files by default. You can do most of the file manipulation using a file object.
Objects
We introduced objects in the earlier lab, but we will review it again. Since Python is an object oriented programming language, almost everything in Python is an object with its data and methods.
https://www.w3schools.com/python/python_classes.asp
Classes
A class is like an object constructor, as a blueprint for creating objects.
https://www.w3schools.com/python/python_classes.aspThe open() function
Working with text files is easy in Python. The first step is to create a file object corresponding to a file.
This is done using the open() function.
open() returns a file object, and is most commonly used with two arguments:
open(filename, mode)
The mode parameter is either the string "r" (read) or "w" (write), depending on whether we want to read from the file or write to the file.
For example, to open a file called "input.txt" for reading, we can do the following:
infile = open("input.txt", "r")
Now we can use the file object infile to read the contents of input.txt.
The input file name is input.txt. The .txt file extension means it is a text file.
Python provides three related operations for reading information from a file:
file_object.read()
Reads the entire content of the file as a single (potentially large, multi-line) string.
file_object.readline()
readline() method returns the next line of the file. That is all text up to and including the next newline character.
What is the newline character?
The newline character '\n' is used to mark the end of a line and the beginning of a new line.
https://www.freecodecamp.org/news/python-new-line-and-how-to-python-print-without-a-newline/file_object.readlines()
Returns all the lines in a file in the format of a list where each element is a line in the file. Each list item is a single line including the newline character at the end.
A list of examples on file read(), readline(), and readlines() methods are here:
https://www.guru99.com/python-file-readline.html https://www.w3schools.com/python/ref_file_readlines.aspReading data from a text file
It is critically important that the data file be a text file. It cannot be a MicroSoft Word file. There are files methods only work for text files. For example, loadtxt() method uses a line feed (LF) to end a line. MicroSoft Word file may include a carriage return (CR) which confuses loadtxt() method.
close()
File method close() closes the opened file. A closed file cannot be read or written any more.
It is a good practice to use the close() method to close a file.
If you are using replit, you can add a file and name it input.txt.
If you are not using desktop Python, create a text file "input.txt" using Notepad, Notepad++ (for PCs) or Sublime, BBEdit (for Macs) that have the following values.
230 500 100 25 600
The following Python program opens the input file input.txt you just created, and output the file content on the monitor.
Note: The input.txt file and the Python program file must be placed in the same directory/folder.1. read()
"""
This program opens an input file "input.txt".
The entire contents of the input.txt is read as one large
string and stored in the variable data.
Print(data) will display the contents of input.txt on the monitor.
"""
def main():
infile = open("input.txt", 'r')
data = infile.read()
print(data)
infile.close()
main()
The output is listed below.
230 500 100 25 600
2. readline()
readline() reads only one complete line from the file.
def main():
infile = open("input.txt", 'r')
data = infile.readline()
print(data)
#print(data[:-1])
infile.close()
main()
Pay attention to line #4 and line #5. Try it out, what is the difference between the two output?
The following example prints out the first three lines of the input file input.txt.
def main():
infile = open("input.txt", 'r')
for i in range(3):
data = infile.readline()
print(data[:-1]) #The use of slicing to remove the newline character at the end of the line.
#print(data) print() automatically outputs a newline,
#so it will output an extra blank line between the lines of the file.
infile.close()
main()
The output is listed below.
230 500 100
3. readlines()
To read all the lines from a given file, readlines() reads all the contents from the given file and save the output in a list.
def main():
infile = open("input.txt", 'r')
data = infile.readlines()
print(data)
infile.close()
main()
Here is the output as a list.
['230\n', '500\n', '100\n', '25\n', '600']
You can also loop through the entire contents of a file like the following since Python treats the file as a sequences of lines.
def main():
infile = open("input.txt", 'r')
for data in infile:
print(data[:-1])
infile.close()
main()
The following example reads each number from the input file and outputs the sum of all numbers on the screen. We can read all the numbers one by one using for i in data and sum up all the numbers,
def main():
infile = open("input.txt", 'r')
sum = 0
for i in infile:
number = float(i) #converts string from the input file into numeric float number.
sum = sum + number
print("The total is:", sum)
infile.close()
main()
We can also use the readlines() method which returns a list containing each line in the file as a list item.
def main():
infile = open("input.txt", 'r')
num_file = infile.readlines()
sum = 0
for i in num_file:
number = float(i)
sum = sum + number
print("The total is:", sum)
infile.close()
main()
File Loops
The lines of a file can be read one at a time using readline() method since it reads the next line from a file as a string. At the end of a file, readline() returns an empty string, which is the sentinel value being used as an end-of-file (EOF) loop.
def main():
infile = open("input.txt", 'r')
data = infile.readline() #read in the first value
sum = 0
while data != "":
sum = sum + float(data)
data = infile.readline() #read in the next value
print("The total is:", sum)
infile.close()
main()
Input file input.txt has the following numbers in it.
230.78 500.34 100.50 25 600
You can download a copy of Grades.csv here.