Summary

Python allows you to read and write to files. The Python file object, which is built into the language, supports the following methods:

  • read()— read in data from a file

  • readline()— read a line of data from a file

  • readlines()— read all the lines in the file and return the lines as a list of strings

  • write()— write data to a file

  • writelines()— write a sequence of lines to a file

  • seek()— move to a certain point in a file

  • tell()— determine the current location in the file

Python makes working with files straightforward. In addition, its pickle and cPickle modules allow the persisting of objects to a file or string and make for speedy development of persisted class data.

In this chapter, we expanded on the address book example from Chapter 4. We ...

Get Python Programming with the Java™ Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.