This course will become read-only in the near future. Tell us at if that is a problem.

Working with Files

An important part of programming is knowing how to work directly with files. Lets review the following Pythonic tools for working with files:

  • open()
  • read()
  • readline() and readlines()
  • write()
  • close()
  • the Pickle module


The open() function takes a filename and path as input and returns a file object.

f = open('/path/to/file', 'r')

The second parameter for the open() function is the file permission mode and is entered in the form of read (r), write (w), or append (a). If no mode parameter is passed, the mode defaults to read. To open binary files, append 'b' to the mode parameter, e.g. read binary ('rb').


The read() method will return the contents of the file as a string. Be careful that your computer has enough memory to read the entire file at once. read() accepts an optional parameter called 'size' that restricts Python to reading a specific portion of the file.

readline() and readlines()

The readline() and readlines() methods read one and multiple lines of a file respectively.

readline() returns one line of the file as a string with a new line character ( '\n' ) at the end. If readline() returns an empty string, the end of the file has been reached.

>>> f.readline()

'This is the first line.\n'

>>> f.readline()

'This is the second line.\n'

>>> f.readline()

' '

readlines() returns all of the lines in a file as a list. Each line in the list will have a newline character ( '\n' ) at the end.

>>> f.readlines()

['This is the first line\n', 'This is the second line\n']


The write() method takes a string as input and writes it to a file. Other data types must be converted to strings before they can be written to a file.

>>> data = 'This string will now be written to a file.'

>>> f.write(data)



When you are done reading from or writing to a file you can close() the file and free memory.


After closing a file, attempts to access the file will fail.


Traceback (most recent call last):
  File "<pyshell#14>", line 1, in <module>
ValueError: I/O operation on closed file.

Pickle module

The Pickle module enables developers to create persistent data objects. Any object or data in Python, even sometimes Python code, can be written to files using Pickle.

Pickled objects are converted to string representations and then unpickled later by converting them to their original types.

>>> import pickle

>>> pickle_object = ['Some data to be pickled', 12345, ('apples', 'butter')]

>>> pickle_file = open('pickle_file', 'wb')

>>> pickle.dump(pickle_object, pickle_file)

To retrieve pickled data, use pickle.load().

>>> pickled_data = open('pickled_file', 'rb')

>>> de_pickled = pickle.load(pickled_data)

>>> print(de_pickled)

['Some data to be pickled', 12345, ('apples', 'butter')]


Learning Resources

Python for Informatics:

Python Docs


Please complete the learning excercises in the Python for Informatics textbook. Post your code work to the web and a link to your files in this discussion.

Task Discussion

  • Fer said:

    on Nov. 7, 2013, 10:34 a.m.
  • Wouter Tebbens said:

    Here are the three exercises from chapter 7:

    on March 29, 2013, 2:04 p.m.

    Anonym said:

    I am away for a short period. I will endeavour to respond to your email when I return. If you don't hear from me in a couple of weeks or so, please send me a reminder.
    on March 29, 2013, 2:10 p.m. in reply to Wouter Tebbens
  • Rob said:

    Chapter 7 exercises:


    on Sept. 23, 2012, 9:27 a.m.
  • saravanan said:

    Chapter 7  completed.

    on Sept. 14, 2012, 10:31 a.m.
  • ionut.vlasin said:

    Exercise 7.1 Write a program to read through a file and print the contents of the file (line by
    line) all in upper case. Executing the program will look as follows:
    for line in fhand:
        print str.upper(line)

    Exercise 7.2 Write a program to prompt for a file name, and then read through the file and look
    for lines of the form:
    When you encounter a line that starts with “X-DSPAM-Confidence:” pull apart the line to
    extract the floating point number on the line. Count these lines and the compute the total of
    the spam confidence values from these lines. When you reach the end of the file, print out the
    average spam confidence.
    for line in fhand:
        if words[0]=='X-DSPAM-Confidence:':
        elif words[0]!='X-DSPAM-Confidence:':
    print "Count=",count,"Avg spam confidence=",sp   

    Sometimes when programmers get bored or want to have a bit of fun, they add
    a harmless Easter Egg to their program (
    Modify the program that prompts the user for the file name so that it prints a funny message
    when the user types in the exact file name ’na na boo boo’. The program should behave normally
    for all other files which exist and don’t exist.
    a=  raw_input('Enter a file name: ')
    if a == 'na na boo boo':
        print " NA NA BOO BOO TO YOU - You have been punk'd  "
    fhand = open(str(a))
    count = 0
    for line in fhand:
            if line.find('Subject:') != -1:
                count += 1
    print 'There were %d subject lines in %s' % (count, a)

    on July 11, 2012, 4:40 a.m.
  • pannix said:

    on May 17, 2012, 11:48 a.m.
  • gnuisance said:

    on March 10, 2012, 3:28 p.m.
  • RobG717 said:

    Here are mine.  I like to use the 'with' statement for opening files, as it makes the try...except blocks a little more tidy.  It also prints out the specific IOError exception, if there is one, thus providing a little more feedback to the user about what might have gone wrong.

    on Feb. 29, 2012, 11:11 a.m.
  • Ken Doman said:

    My answers for Chapter 7 here.

    on Jan. 17, 2012, 1:44 p.m.
  • Sage Ross said:

    Here's mine:

    It's for Python 3, and instead of using local files, it prompts for the url of a file and fetches it from the web.

    on Jan. 3, 2012, 9:44 a.m.
  • Evita said:

    on Oct. 13, 2011, 2:46 p.m.
  • Mars83 said:

    on Oct. 7, 2011, 1:31 p.m.

    Evita said:

    yours answers cover every aspect! very good work! :)

    on Oct. 13, 2011, 2:50 p.m. in reply to Mars83

    Mars83 said:

    I didn't visit this page for a long time, but thank you smiley

    on Sept. 11, 2012, 5:59 p.m. in reply to Evita
  • dean said:

    My answers:

    on Oct. 3, 2011, 11:54 a.m.
  • Anonym said:


    Any ideas as to how I can improve my code?

    on July 4, 2011, 5:54 p.m.
  • Sudaraka Wijesinghe said:

    My py4int exercise 7 code

    Exercise 7.1

    fname = raw_input('Enter the file name: ')
        fh = open(fname)
        print 'File cannot be opened:', fname
    for line in fh :
        line = line.rstrip()
        print line.upper()


    Exercise 7.2

    fname = raw_input('Enter the file name: ')
        fh = open(fname)
        print 'File cannot be opened:', fname
    spam_lines = 0
    spam_total = 0.0
    for line in fh :
        if not line.startswith('X-DSPAM-Confidence:') :
        colon_pos = line.find(':')
        spam_total = spam_total + float(line[colon_pos+1:])
        spam_lines = spam_lines + 1
    print 'Average spam confidence:', spam_total/spam_lines

    Exercise 7.3

    fname = raw_input('Enter the file name: ')
    if 'na na boo boo' == fname :
        print "NA NA BOO BOO TO YOU - You have been punk'd!"
        fh = open(fname)
        print 'File cannot be opened:', fname
    count = 0
    for line in fh :
        if line.startswith('Subject:') :
            count = count +1
    print 'There were', count, 'subject lines in', fname

    on June 26, 2011, 4:14 a.m.
  • Pein Junior said:

    Chapter 7 -

    on June 19, 2011, 12:57 a.m.
  • Vladimir Támara Patiño said:


    fname = raw_input('Enter a file name: ')
        fhand = open(fname)
        print 'File cannot be opened:', fname
    for line in fhand:
        print line.rstrip().upper()


    fname = raw_input('Enter the file name: ')
        fhand = open(fname)
        sum = 0.0
        num = 0
        nline = 0
        for line in fhand:
            nline = nline + 1
            if line.startswith('X-DSPAM-Confidence:') :
                a = line.split(':')
                a = line.split(':')
                    sum = sum + float(a[1])
                    num = num + 1
                    print 'Missing floating point number in line', nline
        if num > 0:
            print 'Average', sum/num
            print 'Not lines with X-DSPAM_Confidence found in', fname
        print 'File cannot be opened:', fname


    fname = raw_input('Enter the file name: ')
    if fname == 'na na boo boo':
        print "NA NA BOO BOO TO YOU - You have been punk'd!"
            fhand = open(fname)
            count = 0
            for line in fhand:
                if line.startswith('Subject:') :
                    count = count + 1
            print "There were", count, "subject lines in", fname
            print 'File cannot be opened:', fname

    on June 13, 2011, 11:09 a.m.