Chapter 11. File and Text Operations

This chapter covers issues related to files and filesystems in Python. A file is a stream of text or bytes that a program can read and/or write; a filesystem is a hierarchical repository of files on a computer system.

Other Chapters That Also Deal with Files

Files are a crucial concept in programming: so, although this chapter is one of the largest in the book, other chapters also have material relevant to handling specific kinds of files. In particular, Chapter 12 deals with many kinds of files related to persistence and database functionality (CSV files in Chapter 12, JSON files in “The json Module”, pickle files in “The pickle Module”, shelve files in “The shelve Module”, DBM and DBM-like files in “The dbm Package”, and SQLite database files in “SQLite”), Chapter 22 deals with files in HTML format, and Chapter 23 deals with files in XML format.

Files and streams come in many flavors. Their contents can be arbitrary bytes, or text. They may be suitable for reading, writing, or both, and they may be buffered, so that data is temporarily held in memory on the way to or from the file. Files may also allow random access, moving forward and back within the file, or jumping to read or write at a particular location in the file. This chapter covers each of these topics.

In addition, this chapter also covers the polymorphic concept of file-like objects (objects that are not actually files but behave to some extent like files), modules that deal ...

Get Python in a Nutshell, 4th Edition 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.