Book Image

The Python Apprentice

By : Robert Smallshire, Austin Bingham
Book Image

The Python Apprentice

By: Robert Smallshire, Austin Bingham

Overview of this book

Experienced programmers want to know how to enhance their craft and we want to help them start as apprentices with Python. We know that before mastering Python you need to learn the culture and the tools to become a productive member of any Python project. Our goal with this book is to give you a practical and thorough introduction to Python programming, providing you with the insight and technical craftsmanship you need to be a productive member of any Python project. Python is a big language, and it’s not our intention with this book to cover everything there is to know. We just want to make sure that you, as the developer, know the tools, basic idioms and of course the ins and outs of the language, the standard library and other modules to be able to jump into most projects.
Table of Contents (21 chapters)
Title Page
Credits
About the Authors
www.PacktPub.com
Customer Feedback
Preface
12
Afterword – Just the Beginning

Chapter 9. Files and Resource Management

Reading and writing files is a key part of what many real-world programs do. The notion of a file, however, is somewhat abstract. In some cases a file might mean collection of bytes on a hard disk; in others cases it might mean, for example, an HTTP resource on a remote system. These two entities share some behavior. For example, you can read a sequence of bytes from each. At the same time, they're not identical. You can, for example, generally write bytes back to a local file while you can't do that with HTTP resources.

In this chapter we'll look at Python's basic support for working with files. Since dealing with local files is both common and important, we'll focus primarily on working with them. Be aware, though, that Python and its ecosystem of libraries provides similar file-like APIs for many other kinds of entities, including URI-based resources, databases, and many other sources of data. This use of a common API is very convenient and makes...