Cover page images (keyboard)

I/O

Sue Evans & James MacGlashan

Adapted from the CS1 Course at Swarthmore College by Lisa Meeden

Hit the space bar for next slide

Learning Outcomes

File Input/Output

Writing to a File

Appending a File

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

What is "r+" mode?

"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 ?

Double Space

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.

File Exercise

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.