This is the P2PU Archive. If you want the current site, go to www.p2pu.org!

Python Programming 101

Working with Files

Brylie Oxley's picture
Mon, 2011-06-06 05:01

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.

Comments

Exercises chapter 4:

Johan Mares's picture
Johan Mares
Tue, 2011-06-07 17:43

Exercises chapter 4: http://pastie.org/2032889
Not mentioned in book, but may come in handy is appending text to an existing file instead of creating a new file.
f = open('filename.txt', 'a')