Skip to Content
Learning Python
book

Learning Python

by Mark Lutz, David Ascher
April 1999
Beginner
384 pages
11h 15m
English
O'Reilly Media, Inc.
Content preview from Learning Python

Files

Hopefully, most readers are familiar with the notion of files—named storage compartments on your computer that are managed by your operating system. Our last built-in object type provides a way to access those files inside Python programs. The built-in open function creates a Python file object, which serves as a link to a file residing on your machine. After calling open, you can read and write the associated external file, by calling file object methods.

Compared to types we’ve seen so far, file objects are somewhat unusual. They’re not numbers, sequences, or mappings; instead, they export methods only for common file processing tasks. Technically, files are a prebuilt C extension type that provides a thin wrapper over the underlying C stdio filesystem; in fact, file object methods have an almost 1-to-1 correspondence to file functions in the standard C library.

Table 2.10 summarizes common file operations. To open a file, a program calls the open function, with the external name first, followed by a processing mode ('r' means open for input, 'w' means create and open for output, 'a' means open for appending to the end, and others we’ll ignore here). Both arguments must be Python strings.

Table 2-10. Common File Operations

Operation

Interpretation

output = open('/tmp/spam', 'w')

Create output file ('w' means write)

input = open('data', 'r')

Create input file ('r' means read)

S = input.read()

Read entire file into a single string

S = input.read(N)

Read N bytes (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.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Learning Python

Learning Python

Fabrizio Romano

Publisher Resources

ISBN: 1565924649Catalog PageErrata