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: ♦♦♦♢♢
-❝ FIXME ❞
— FIXME +❝ A nine mile walk is no joke, especially in the rain. ❞
— Harry Kemelman, The Nine Mile Walk
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.
-
FIXME write(), writelines(), .writeable -
FIXME +
+
+
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'
mode parameter contains a 'b'.
+mode, which reflects the mode parameter you passed into the open() function.
+name attribute, just like file objects for text files.
+encoding attribute. That makes sense, right? Youre 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
read() method takes the number of bytes to read, not the number of characters.
+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.
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]