From 9b9c2a92a5f3f4e109912f39de480f57fb89f615 Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Sun, 19 Jul 2009 15:14:40 -0400 Subject: [PATCH] finished files chapter --- dip3.css | 5 +++++ files.html | 36 ++++++++++++++++++------------------ installing-python.html | 5 ----- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/dip3.css b/dip3.css index f103835..e1fedb2 100755 --- a/dip3.css +++ b/dip3.css @@ -44,6 +44,7 @@ Classname Legend .pp = "pretty print" = apply syntax highlighting to this code block .pf = "padded frame" = black border with internal padding .fr = "framed" = black border, no padding +.ss = "screenshot" = image, floated right, with margin .note = "note/caution/important" = indented block for tips/gotchas/language comparisons .baa = "best available ampersand" = wrapper block for ampersands @@ -160,6 +161,10 @@ form div, #level { line-height:1; margin:0.7em 0; } +.ss { + float: right; + margin: 0 0 1.75em 1.75em; +} /* links */ diff --git a/files.html b/files.html index aadfad4..d794229 100644 --- a/files.html +++ b/files.html @@ -16,7 +16,7 @@ body{counter-reset:h1 12}

Difficulty level: ♦♦♦♢♢

Files

-

FIXME
— FIXME +

A nine mile walk is no joke, especially in the rain.
— Harry Kemelman, The Nine Mile Walk

 

Diving In

@@ -296,16 +296,14 @@ ValueError: I/O operation on closed file.

Did you notice the encoding parameter that got passed in to the open() function while you were opening a file for writing? It’s important; don’t ever leave it out! As you saw in the beginning of this chapter, files don’t contain strings, they contain bytes. Reading a “string” from a text file only works because you told Python what encoding to use to read a stream of bytes and convert it to a string. Writing text to a file presents the same problem in reverse. You can’t write characters to a file; characters are an abstraction. In order to write to the file, Python needs to know how to convert your string into a sequence of bytes. The only way to be sure it’s performing the correct conversion is to specify the encoding parameter when you open the file for writing. -

Write A Little, Write A Lot

- -

FIXME write(), writelines(), .writeable -

Binary Files

-

FIXME +

my dog Beauregard + +

Not every file contains text. Some of them contain pictures of my dog.

->>> an_image = open('examples/beauregard-100x100.jpg', mode='rb')        
+>>> an_image = open('examples/beauregard.jpg', mode='rb')                
 >>> an_image.mode                                                        
 'rb'
 >>> an_image.name                                                        
@@ -315,22 +313,24 @@ ValueError: I/O operation on closed file.
   File "<stdin>", line 1, in <module>
 AttributeError: '_io.BufferedReader' object has no attribute 'encoding'
    -
  1. FIXME -
  2. -
  3. -
  4. +
  5. Opening a file in binary mode is simple but subtle. The only difference from opening it in text mode is that the mode parameter contains a 'b'. +
  6. The file object you get from opening a file in binary mode has many of the same attributes, including mode, which reflects the mode parameter you passed into the open() function. +
  7. File objects for binary files also have a name attribute, just like file objects for text files. +
  8. Here’s one difference, though: the file object for a binary file has no encoding attribute. That makes sense, right? You&#re reading (or writing) bytes, not strings, so there’s no conversion for Python to do. What you get out of a binary file is exactly what you put into it, no conversion necessary.
+

Did I mention you’re reading bytes? Oh yes you are. +

 # continued from the previous example
->>> an_image.tell()       
+>>> an_image.tell()
 0
->>> data = image.read(3)  
+>>> data = image.read(3)    
 >>> data
 b'\xff\xd8\xff'
->>> type(data)            
+>>> type(data)              
 <class 'bytes'>
->>> an_image.tell()
+>>> an_image.tell()         
 3
 >>> an_image.seek(0)
 0
@@ -338,9 +338,9 @@ AttributeError: '_io.BufferedReader' object has no attribute 'encoding'>>> len(data)
 3150
    -
  1. FIXME -
  2. -
  3. +
  4. Like text files, you can read binary files a little bit at a time. But there’s a crucial difference… +
  5. …you’re reading bytes, not strings. Since you opened the file in binary mode, the read() method takes the number of bytes to read, not the number of characters. +
  6. That means that there’s never an unexpected mismatch between the number you passed into the read() method and the position index you get out of the tell() method. The read() method reads bytes, and the seek() and tell() methods track the number of bytes read. For binary files, they’ll always agree.

File-like Objects

diff --git a/installing-python.html b/installing-python.html index dd48523..2142f4f 100755 --- a/installing-python.html +++ b/installing-python.html @@ -9,7 +9,6 @@ body{counter-reset:h1 0} .i{list-style:none;margin:0;padding:0} #which{padding-top:1.75em} h2,.i>li{clear:both} -.i li .ss{float:right;margin:0 0 1.75em 1.75em} @@ -199,10 +198,6 @@ Type "help", "copyright", "credits" or "license" for more information.

[Mac Python Shell, a graphical interactive shell for Python]

The Python Shell is where you will spend most of your time exploring Python. Examples throughout this book will assume that you can find your way into the Python Shell. -

[Skip to using the Python Shell]