Sue Evans & James MacGlashan
Adapted from the CS1 Course at Swarthmore College by Lisa Meeden
Hit the space bar for next slide
This is line 1 This is line 2 This is line 3
>>> file = open("file.txt", "r") >>> firstLine = file.readline() >>> firstLine 'This is line 1\n'
>>> file = open("file.txt", "r") >>> file.readline() 'This is line 1\n' >>> file.readline() 'This is line 2\n' >>> file.readline() 'This is line 3\n'
>>> file = open("file.txt", "r") >>> print file.readline() This is line 1 >>> print file.readline() This is line 2 >>> print file.readline() This is line 3 >>>
>>> file = open("file.txt", "r") >>> file.readline().strip() 'This is line 1' >>> file.readline().strip() 'This is line 2' >>> file.readline().strip() 'This is line 3'
>>> file = open("file.txt", "r") >>> file.readline() 'This is line 1\n' >>> file.readline() 'This is line 2\n' >>> file.readline() 'This is line 3\n' >>> file.readline() ''
>>> file = open("file.txt", "r") >>> for line in file: ... line ... 'This is line 1\n' 'This is line 2\n' 'This is line 3\n'
>>> file = open("file.txt", "r") >>> for line in file: ... print line ... This is line 1 This is line 2 This is line 3 >>>
>>> file = open("file.txt", "r") >>> for line in file: ... print line.strip() ... This is line 1 This is line 2 This is line 3 >>>
>>> file = open("file.txt", "r") >>> for line in file: ... print line.strip() ... This is line 1 This is line 2 This is line 3 >>> file.close()
>>> file = open("newfile.txt", "w") >>> file.write("New file line 1") >>> file.write("New file line 2") >>> file.close()
New file line 1New file line 2
>>> file = open("newfile.txt", "w") >>> file.write("New file line 1\n") >>> file.write("New file line 2\n") >>> file.write("New file line 3\n") >>> file.close()
New file line 1 New file line 2 New file line 3
>>> file = open("newfile.txt", "w") >>> file.write(5) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: argument 1 must be string or read-only character buffer, not int
# Filename: gradeReport.py # Authors: Sue Evans & James MacGlashan # Date: 8/6/09 # Section: 12 # Email: bogar@cs.umbc.edu # Description: This program generates grade # report files for two students. # generateGradeReport() gets four homework grades # from the user, calculates their average, # generates a unique filename for each student, # and writes a grade report out to a file. # # Input: a student's name, studentName # Output: none # Effect: A grade report file is produced. def generateGradeReport(studentName) : # generate personalized prompts for each homework prompt1 = "Enter " + studentName + "'s grade for HW1: " prompt2 = "Enter " + studentName + "'s grade for HW2: " prompt3 = "Enter " + studentName + "'s grade for HW3: " prompt4 = "Enter " + studentName + "'s grade for HW4: " # get input from user grade1 = input(prompt1) grade2 = input(prompt2) grade3 = input(prompt3) grade4 = input(prompt4) average = (grade1 + grade2 + grade3 + grade4) / 4.0 # generate a unique filename for each student filename = studentName + ".txt" # write the information to the file file = open(filename, "w") file.write("HW1: " + str(grade1) + "\n") file.write("HW2: " + str(grade2) + "\n") file.write("HW3: " + str(grade3) + "\n") file.write("HW4: " + str(grade4) + "\n") file.write("Average: " + str(average) + "\n") file.close() def main() : generateGradeReport("Alice") print generateGradeReport("Bob") main()
Enter Alice's grade for HW1: 93.0 Enter Alice's grade for HW2: 84.0 Enter Alice's grade for HW3: 87.0 Enter Alice's grade for HW4: 90.0 Enter Bob's grade for HW1: 89.0 Enter Bob's grade for HW2: 91.0 Enter Bob's grade for HW3: 80.0 Enter Bob's grade for HW4: 91.0
HW1: 93.0 HW2: 84.0 HW3: 87.0 HW4: 90.0 Average: 88.5
HW1: 89.0 HW2: 91.0 HW3: 80.0 HW4: 91.0 Average: 87.75
You can add new data to the end of a file using append mode, "a".
So, if you have a file named sample.txt with these contents:
Sample - line 1 Sample - line 2 Sample - line 3
We can open it in append mode and write some more lines to the file.
>>> file = open("sample.txt", "a") >>> file.write("1st line appended\n") >>> file.write("2nd line appended\n") >>> file.close() >>>
Let's look at sample.txt now.
Sample - line 1 Sample - line 2 Sample - line 3 1st line appended 2nd line appended
What happens if you try to append a file that doesn't exist ?
A new file will be made with the filename you used in the call to
open()
"r+" mode allows you to both read from and write to a file.
When using this mode, you can read through a file until you find the place where you want to start writing, and then write to the file.
Here is sample.txt currently:
Sample - line 1 Sample - line 2 Sample - line 3 1st line appended 2nd line appended
Let's try using "r+".
>>> file = open("sample.txt", "r+") >>> line = file.readline() >>> line 'Sample - line 1\n' >>> line = file.readline() >>> line 'Sample - line 2\n' >>> file.write("Writing a new line\n") >>> file.close() >>>
Sample - line 1 Sample - line 2 Writing a new line line appended 2nd line appended
Oh, my! What happened here ?
I'll fix the sample.txt file back to the way it was and we can try our experiment.
Sample - line 1 Sample - line 2 Sample - line 3 1st line appended 2nd line appended
>>> file = open("sample.txt", "r+") >>> line = file.readline() >>> line 'Sample - line 1\n' >>> line = file.readline() >>> line 'Sample - line 2\n' >>> line = file.readline() >>> line 'Sample - line 3\n' >>> file.write("Exact length line\n") >>> file.close() >>>
Sample - line 1 Sample - line 2 Sample - line 3 Exact length line 2nd line appended
We were right. When we write to the file, what is written has to be exactly the same length as the original text that gets replaced, if it is to leave the remainder of the file unchanged.
This behavior severely limits the usefulness of this mode.
When could you use this mode ?
Let's write a program that will read in a file that is single-paced and write it out to a file double spacing it.
# File: doubleSpace.py # Author: Sue Evans # Date: 10/1/09 # Section: All # EMail: bogar@cs.umbc.edu # # This program reads in lines from a file that # is single spaced and writes out to a file so # that it will be double-spaced. import string # printGreeting() prints an explanation of the # program for the user # Inputs: None # Outputs: None def printGreeting(): print print "This program will read in the lines from a file that" print "is single-spaced and write those same lines out to" print "a file that will be double-spaced" print def main(): printGreeting() # get the filename from the user filename = raw_input("Enter the name of the file to double-space : ") # construct outfile name list = string.split(filename, '.') outFilename = list[0] + '.ds.' + list[1] # open infile and outfile infile = open(filename, 'r') outfile = open(outFilename, 'w') # process each line in the file for line in infile: double = line + '\n' outfile.write(double) # close the files infile.close() outfile.close() main()
Let's look at both files
Here's sample.txt
This is line 1. This is line 2. This is line 3. This is the last line.
And here's sample.ds.txt
This is line 1. This is line 2. This is line 3. This is the last line.
Write a similar program that will read in lines from one file and write out those same lines to another file with the lines numbered. The line numbers should be right-justified in a total field width of 2.
Here's the contents of ioExercise.txt
This is line one. This is line two. This is line three. This is line four This is line five This is line six This is line seven This is line eight. This is line nine. This is line ten. This is line eleven. This is line twelve.
Here's the contents of ioExercise.num.txt
1 This is line one. 2 This is line two. 3 This is line three. 4 This is line four 5 This is line five 6 This is line six 7 This is line seven 8 This is line eight. 9 This is line nine. 10 This is line ten. 11 This is line eleven. 12 This is line twelve.