15.04.2013 Views

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

9.1. File Objects<br />

File objects can be used to access not only normal disk files, but also any other type of "file" that uses<br />

that abstraction. Once the proper "hooks" are installed, you can access other objects with file-style<br />

interfaces in the same manner you would access normal files.<br />

You will find many cases where you are dealing with "file-like" objects as you continue to develop your<br />

<strong>Python</strong> experience. Some examples include "opening a URL" for reading a Web page in real-time and<br />

launching a command in a separate process and communicating to and from it like a pair of<br />

simultaneously open files, one for write and the other for read.<br />

The open() built-in function (see below) returns a file object that is then used for all succeeding<br />

operations on the file in question. There are a large number of other functions that return a file or filelike<br />

object. One primary reason for this abstraction is that many input/output data structures prefer to<br />

adhere to a common interface. It provides consistency in behavior as well as implementation. Operating<br />

systems like Unix even feature files as an underlying and architectural interface for communication.<br />

Remember, files are simply a contiguous sequence of bytes. Anywhere data need to be sent usually<br />

involves a byte stream of some sort, whether the stream occurs as individual bytes or blocks of data.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!