200 likes | 348 Views
Lecture 6. Using files. Opening a file. < filevar > = open( <name of file>, <mode>) <name of file> is the string describing where the file is stored <mode> is either “r” or “w” depending on whether you intend to read from or write to that file. Example: i nfile = open(“numbers.txt”, “r”).
E N D
Lecture 6 Using files
Opening a file • <filevar> = open( <name of file>, <mode>) • <name of file> is the string describing where the file is stored • <mode> is either “r” or “w” depending on whether you intend to read from or write to that file. • Example: • infile = open(“numbers.txt”, “r”)
Ways to read a file • infile.read() # returns the entire file in one huge string • infile.readline() # returns up and including ‘\n’ compare to raw_input which discards the newline character. • infile.readlines() #returns a list of the remaining lines; each is a single line including ‘\n’ • infile.close() #disconnects the variable from the file
fin = open("sample.txt", "r") for i in range(3): aLine = fin.readline(); print aLine fin.close() fin = open("sample.txt", "r") for aLine in fin.readlines(): print aLine #try print aLine[:-1] fin.close()
The typical pattern for reading • Processing a file line by line is so common that Python provides a nice shortcut • fin = open("sample.txt", "r") • for line in fin: • #process the line • print line[:-1] • fin.close
Writing to a file • Open the file for writing • fout = open(“output.txt”, “w”) • Print a string to the file – you must attach a newline if you want one! • <filevar>.write(<string>) • fout.write(“This is a test\n”)
Class Problem • The file on my disk called data.txt looks like this Mary Smith 97 42 88 Tim Wilson 88 90 78 etc There are a bunch of lines beginning with a first and last name and then three test grades. I want to produce a file average.txt containing the names and averages, nicely formatted.
Algorithm • Open a file for reading • Open a file for writing • Print the Headings • In the loop • Chop up a line of code • Convert the grades to numbers and calculate the average • Write the first and last names and the average to the file • Close all files
Open a file for reading • Open a file for writing
Open a file for reading • Open a file for writing • f_in = open("data.txt", "r") • f_out = open("average.txt", "w")
Open a file for reading • Open a file for writing • f_in = open("data.txt", "r") • f_out = open("average.txt", "w") • Print the Headings
Open a file for reading • Open a file for writing • f_in = open("data.txt", "r") • f_out = open("average.txt", "w") • Print the Headings • f_out.write( "%-20s%-20s%s\n" % ("Last Name", "First Name", "Average"))
Open a file for reading • Open a file for writing • f_in = open("data.txt", "r") • f_out = open("average.txt", "w") • Print the Headings • f_out.write( "%-20s%-20s%s\n" % ("Last Name", "First Name", "Average")) • In the loop • Chop up a line of code
Open a file for reading • Open a file for writing • f_in = open("data.txt", "r") • f_out = open("average.txt", "w") • Print the Headings • f_out.write( "%-20s%-20s%s\n" % ("Last Name", "First Name", "Average")) • In the loop • Chop up a line of code • for line in f_in: • data = line.split()
Open a file for reading • Open a file for writing • f_in = open("data.txt", "r") • f_out = open("average.txt", "w") • Print the Headings • f_out.write( "%-20s%-20s%s\n" % ("Last Name", "First Name", "Average")) • In the loop • Chop up a line of code • for line in f_in: • data = line.split() • Convert the grades to numbers and calculate the average
Open a file for reading • Open a file for writing • f_in = open("data.txt", "r") • f_out = open("average.txt", "w") • Print the Headings • f_out.write( "%-20s%-20s%s\n" % ("Last Name", "First Name", "Average")) • In the loop • Chop up a line of code • for line in f_in: • data = line.split() • Convert the grades to numbers and calculate the average • average = (int(data[2]) + int(data[3]) + int(data[4]))/3.0
Open a file for reading • Open a file for writing • f_in = open("data.txt", "r") • f_out = open("average.txt", "w") • Print the Headings • f_out.write( "%-20s%-20s%s\n" % ("Last Name", "First Name", "Average")) • In the loop • Chop up a line of code • for line in f_in: • data = line.split() • Convert the grades to numbers and calculate the average • average = (int(data[2]) + int(data[3]) + int(data[4]))/3.0 • Write the first and last names and the average to the file
Open a file for reading • Open a file for writing • f_in = open("data.txt", "r") • f_out = open("average.txt", "w") • Print the Headings • f_out.write( "%-20s%-20s%s\n" % ("Last Name", "First Name", "Average")) • In the loop • Chop up a line of code • for line in f_in: • data = line.split() • Convert the grades to numbers and calculate the average • average = (int(data[2]) + int(data[3]) + int(data[4]))/3.0 • Write the first and last names and the average to the file • f_out.write("%-20s%-20s%6.2f\n" % (data[1], data[0], average))
Open a file for reading • Open a file for writing • f_in = open("data.txt", "r") • f_out = open("average.txt", "w") • Print the Headings • f_out.write( "%-20s%-20s%s\n" % ("Last Name", "First Name", "Average")) • In the loop • Chop up a line of code • for line in f_in: • data = line.split() • Convert the grades to numbers and calculate the average • average = (int(data[2]) + int(data[3]) + int(data[4]))/3.0 • Write the first and last names and the average to the file • f_out.write("%-20s%-20s%6.2f\n" % (data[1], data[0], average)) • Close all files
Open a file for reading • Open a file for writing • f_in = open("data.txt", "r") • f_out = open("average.txt", "w") • Print the Headings • f_out.write( "%-20s%-20s%s\n" % ("Last Name", "First Name", "Average")) • In the loop • Chop up a line of code • for line in f_in: • data = line.split() • Convert the grades to numbers and calculate the average • average = (int(data[2]) + int(data[3]) + int(data[4]))/3.0 • Write the first and last names and the average to the file • f_out.write("%-20s%-20s%6.2f\n" % (data[1], data[0], average)) • Close all files • f_in.close() • f_out.close()