Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 11 ■ ■ ■ Files and Stuff So far we’ve mainly been working with data structures that reside in the interpreter itself. What little interaction our programs have had with the outside world has been through input, raw_input, and print. In this chapter, we go one step further and let our programs catch a glimpse of a larger world: the world of files and streams. The functions and objects described in this chapter will enable you to store data between program invocations and to process data from other programs. FILE-LIKE OBJECTS You will probably run into the term “file-like” repeatedly in your Python career (I’ve used it a few times already). A file-like object is simply one supporting a few of the same methods as a file, most notably either read or write or both. The objects returned by urllib.urlopen (see Chapter 14) are a good example of this. They support methods such as read, readline, and readlines, but not (at the time of writing) methods such as fileno or isatty, for example. Opening Files You can open files with the open function, which has the following syntax: open(name[, mode[, buffering]]) The open function takes a file name as its only mandatory argument, and returns a file object. The mode and buffering arguments are both optional and will be explained in the material that follows. ■Note In earlier versions of Python, open was a separate function, but from version 2.2 onwards, open is the same as file, the file type. So when you call it as a function, you are actually using the file constructor to create a file object. 255
256 CHAPTER 11 ■ FILES AND STUFF So, assuming that you have a text file (created with your text editor, perhaps) called somefile.txt stored in the directory C:\text (or something like ~/text in UNIX), you can open it like this: >>> f = open(r'C:\text\somefile.txt') If the file doesn’t exist, you may see an exception traceback like this: Traceback (most recent call last): File "", line 1, in ? IOError: [Errno 2] No such file or directory: "C:\\text\\somefile.txt" You’ll see what you can do with such file objects in a little while, but first, let’s take a look at the other two arguments of the open function. The Mode Argument If you use open with only a file name as a parameter, you get a file object you can read from. If you want to write to the file, you have to state that explicitly, supplying a mode. (Be patient— I get to the actual reading and writing in a little while.) The mode argument to the open function can have several values, as summarized in Table 11-1. Table 11-1. Possible Values for the Mode Argument of the open Function Value 'r' 'w' 'a' 'b' '+' Description Read mode Write mode Append mode Binary mode (added to other mode) Read/write mode (added to other mode) Explicitly specifying read mode has the same effect as not supplying a mode string at all. The write mode enables you to write to the file. The '+' can be added to any of the other modes to indicate that both reading and writing is allowed. So, for example, 'r+' can be used when opening a text file for reading and writing. (For this to be useful, you will probably want to use seek as well; see the sidebar “Random Access” later in this chapter.) The 'b' mode changes the way the file is handled. Generally, Python assumes that you are dealing with text files (containing characters). Typically, this is not a problem. But if you are processing some other kind of file (called a binary file) such as a sound clip or an image, you should add a 'b' to your mode: for example, 'rb' to read a binary file.
- Page 234 and 235: CHAPTER 10 ■ ■ ■ Batteries In
- Page 236 and 237: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 238 and 239: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 240 and 241: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 242 and 243: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 244 and 245: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 246 and 247: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 248 and 249: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 250 and 251: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 252 and 253: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 254 and 255: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 256 and 257: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 258 and 259: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 260 and 261: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 262 and 263: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 264 and 265: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 266 and 267: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 268 and 269: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 270 and 271: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 272 and 273: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 274 and 275: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 276 and 277: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 278 and 279: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 280 and 281: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 282 and 283: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 284: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 289 and 290: 258 CHAPTER 11 ■ FILES AND STUFF
- Page 291 and 292: 260 CHAPTER 11 ■ FILES AND STUFF
- Page 293 and 294: 262 CHAPTER 11 ■ FILES AND STUFF
- Page 295 and 296: 264 CHAPTER 11 ■ FILES AND STUFF
- Page 297 and 298: 266 CHAPTER 11 ■ FILES AND STUFF
- Page 299 and 300: 268 CHAPTER 11 ■ FILES AND STUFF
- Page 301 and 302: 270 CHAPTER 12 ■ GRAPHICAL USER I
- Page 303 and 304: 272 CHAPTER 12 ■ GRAPHICAL USER I
- Page 305 and 306: 274 CHAPTER 12 ■ GRAPHICAL USER I
- Page 307 and 308: 276 CHAPTER 12 ■ GRAPHICAL USER I
- Page 309 and 310: 278 CHAPTER 12 ■ GRAPHICAL USER I
- Page 311 and 312: 280 CHAPTER 12 ■ GRAPHICAL USER I
- Page 313 and 314: 282 CHAPTER 12 ■ GRAPHICAL USER I
- Page 316 and 317: CHAPTER 13 ■ ■ ■ Database Sup
- Page 318 and 319: CHAPTER 13 ■ DATABASE SUPPORT 287
- Page 320 and 321: CHAPTER 13 ■ DATABASE SUPPORT 289
- Page 322 and 323: CHAPTER 13 ■ DATABASE SUPPORT 291
- Page 324 and 325: CHAPTER 13 ■ DATABASE SUPPORT 293
- Page 326: CHAPTER 13 ■ DATABASE SUPPORT 295
- Page 329 and 330: 298 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 331 and 332: 300 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 333 and 334: 302 CHAPTER 14 ■ NETWORK PROGRAMM
CHAPTER 11<br />
■ ■ ■<br />
Files and Stuff<br />
So far we’ve mainly been working with data structures that reside in the interpreter itself.<br />
What little interaction our programs have had with the outside world has been through input,<br />
raw_input, and print. In this chapter, we go one step further and let our programs catch a<br />
glimpse of a larger world: the world of files and streams. The functions and objects described<br />
in this chapter will enable you <strong>to</strong> s<strong>to</strong>re data between program invocations and <strong>to</strong> process data<br />
from other programs.<br />
FILE-LIKE OBJECTS<br />
You will probably run in<strong>to</strong> the term “file-like” repeatedly in your <strong>Python</strong> career (I’ve used it a few times already).<br />
A file-like object is simply one supporting a few of the same methods as a file, most notably either read or<br />
write or both. The objects returned by urllib.urlopen (see Chapter 14) are a good example of this. They<br />
support methods such as read, readline, and readlines, but not (at the time of writing) methods such as<br />
fileno or isatty, for example.<br />
Opening Files<br />
You can open files with the open function, which has the following syntax:<br />
open(name[, mode[, buffering]])<br />
The open function takes a file name as its only manda<strong>to</strong>ry argument, and returns a file<br />
object. The mode and buffering arguments are both optional and will be explained in the<br />
material that follows.<br />
■Note In earlier versions of <strong>Python</strong>, open was a separate function, but from version 2.2 onwards, open is<br />
the same as file, the file type. So when you call it as a function, you are actually using the file construc<strong>to</strong>r<br />
<strong>to</strong> create a file object.<br />
255