1 / 20

Lecture 6

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”).

rae
Download Presentation

Lecture 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 6 Using files

  2. 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”)

  3. 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

  4. 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()

  5. 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

  6. 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”)

  7. 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.

  8. 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

  9. Open a file for reading • Open a file for writing

  10. Open a file for reading • Open a file for writing • f_in = open("data.txt", "r") • f_out = open("average.txt", "w")

  11. 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

  12. 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"))

  13. 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

  14. 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()

  15. 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

  16. 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

  17. 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

  18. 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))

  19. 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

  20. 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()

More Related