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 stringwithopen('somefile.txt','rt')asf:data=f.read()# Iterate over the lines of the filewithopen('somefile.txt','rt')asf:forlineinf:# 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 datawithopen('somefile.txt','wt')asf:f.write(text1)f.write(text2)...# Redirected print statementwithopen('somefile.txt','wt')asf:(line1,file=f)(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:
withopen('somefile.txt','rt',encoding='latin-1')