Skip to Content
Python Cookbook, 3rd Edition
book

Python Cookbook, 3rd Edition

by David Beazley, Brian K. Jones
May 2013
Intermediate to advanced content levelIntermediate to advanced
706 pages
14h 42m
English
O'Reilly Media, Inc.
Content preview from Python Cookbook, 3rd Edition

Chapter 5. Files and I/O

All programs need to perform input and output. This chapter covers common idioms for working with different kinds of files, including text and binary files, file encodings, and other related matters. Techniques for manipulating filenames and directories are also covered.

5.1. Reading and Writing Text Data

Problem

You need to read or write text data, possibly in different text encodings such as ASCII, UTF-8, or UTF-16.

Solution

Use the open() function with mode rt to read a text file. For example:

# Read the entire file as a single string with open('somefile.txt', 'rt') as f: data = f.read() # Iterate over the lines of the file with open('somefile.txt', 'rt') as f: for line in f: # process line ...

Similarly, to write a text file, use open() with mode wt to write a file, clearing and overwriting the previous contents (if any). For example:

# Write chunks of text data with open('somefile.txt', 'wt') as f: f.write(text1) f.write(text2) ... # Redirected print statement with open('somefile.txt', 'wt') as f: print(line1, file=f) print(line2, file=f) ...

To append to the end of an existing file, use open() with mode at.

By default, files are read/written using the system default text encoding, as can be found in sys.getdefaultencoding(). On most machines, this is set to utf-8. If you know that the text you are reading or writing is in a different encoding, supply the optional encoding parameter to open(). For example:

with open('somefile.txt', 'rt', encoding='latin-1') 
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Fluent Python, 2nd Edition

Fluent Python, 2nd Edition

Luciano Ramalho

Publisher Resources

ISBN: 9781449357337Errata PageSupplemental Content