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

open()

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

read()

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.

f.read()

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']

write()

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)

42


close()

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

f.close()

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

f.readlines()

Traceback (most recent call last):
  File "<pyshell#14>", line 1, in <module>
    f.readlines()
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

Task

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 7 november 2013 10:34
  • Wouter Tebbens said:

    Here are the three exercises from chapter 7: http://pastebin.com/5AXZT7TN

    on 29 maart 2013 14:04

    Anoniem 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 29 maart 2013 14:10 in reply to Wouter Tebbens
  • Rob said:

    Chapter 7 exercises:

     

    http://pastebin.com/a12RZ94C

    on 23 september 2012 09:27
  • saravanan said:

    Chapter 7  completed.

    on 14 september 2012 10:31
  • 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:
    '''
    fhand=open('mail.txt')
    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:
    X-DSPAM-Confidence:
    0.8475
    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.
    '''
    fhand=open('fis.txt')
    count=0
    sp=0
    for line in fhand:
        words=line.split()
     
        if words[0]=='X-DSPAM-Confidence:':
            count=count+1
            sp=sp+float(words[1])
        elif words[0]!='X-DSPAM-Confidence:':
            continue
    sp=sp/count
    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 (en.wikipedia.org/wiki/Easter_egg_(media)).
    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 11 juli 2012 04:40
  • pannix said:

    on 17 mei 2012 11:48
  • gnuisance said:

    on 10 maart 2012 15:28
  • 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.

    http://pastebin.com/HtL5Sq6W

    on 29 februari 2012 11:11
  • Ken Doman said:

    My answers for Chapter 7 here.

    on 17 januari 2012 13:44
  • Sage Ross said:

    Here's mine:

    http://pastebin.com/AExXE8Nz

    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 3 januari 2012 09:44
  • Evita said:

    on 13 oktober 2011 14:46
  • Mars83 said:

    on 7 oktober 2011 13:31

    Evita said:

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

    on 13 oktober 2011 14:50 in reply to Mars83

    Mars83 said:

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

    on 11 september 2012 17:59 in reply to Evita
  • dean said:

    My answers:

    http://goo.gl/cojbX

    on 3 oktober 2011 11:54
  • Anoniem said:

     

    https://github.com/DamionKing/P2PU/tree/master/Python-Programming-101\

    Any ideas as to how I can improve my code?

    on 4 juli 2011 17:54
  • Sudaraka Wijesinghe said:

    My py4int exercise 7 code

    Exercise 7.1

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

     

    Exercise 7.2

    fname = raw_input('Enter the file name: ')
    
    try:
        fh = open(fname)
    except:
        print 'File cannot be opened:', fname
        exit()
    
    spam_lines = 0
    spam_total = 0.0
        
    for line in fh :
        if not line.startswith('X-DSPAM-Confidence:') :
            continue
    
        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!"
        exit()
    
    try:
        fh = open(fname)
    except:
        print 'File cannot be opened:', fname
        exit()
        
    count = 0
    
    for line in fh :
        if line.startswith('Subject:') :
            count = count +1
    
    print 'There were', count, 'subject lines in', fname
    
    

    on 26 juni 2011 04:14
  • Pein Junior said:

    Chapter 7 -

    http://www.pastie.org/2090140

    on 19 juni 2011 00:57
  • Vladimir Támara Patiño said:

    7.1

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

    7.2

    fname = raw_input('Enter the file name: ')
    try:
        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(':')
                try:
                a = line.split(':')
                try:
                    sum = sum + float(a[1])
                    num = num + 1
                except:
                    print 'Missing floating point number in line', nline
        fhand.close()
        if num > 0:
            print 'Average', sum/num
        else:
            print 'Not lines with X-DSPAM_Confidence found in', fname
    except:
        print 'File cannot be opened:', fname
    
    



    7.3

    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!"
    else:
        try:
            fhand = open(fname)
            count = 0
            for line in fhand:
                if line.startswith('Subject:') :
                    count = count + 1
            fhand.close()
            print "There were", count, "subject lines in", fname
        except:
            print 'File cannot be opened:', fname
            exit()
    
    
    

    on 13 juni 2011 11:09