You are here: Home ‣ Dive Into Python 3 ‣
Difficulty level: ♦♦♦♢♢
❝ FIXME ❞
— FIXME
FIXME
FIXME
open(..., encoding='...') open(..., 'r', encoding='...')
Bytes are bytes; characters are an abstraction. A string is a sequence of Unicode characters. But a file on disk is not a sequence of Unicode characters; a file on disk is a sequence of bytes. So if you read a “text file” from disk, how does Python convert that sequence of bytes into a sequence of characters? It decodes the bytes according to a specific character encoding algorithm, and returns a sequence of Unicode characters, otherwise known as a string.
# on Windows...
>>> file = open('examples/chinese.txt')
>>> a_string = file.read()
Traceback (most recent call last):
File "", line 1, in
File "C:\Python31\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 28: character maps to
>>>
FIXME
Python has a built-in function, open(), for opening a file on disk. The open() function returns a file object, which has methods and attributes for getting information about and manipulating the file.
FIXME
Open files consume system resources, and depending on the file mode, other programs may not be able to access them. It’s important to close files as soon as you’re finished with them.
FIXME checking if a file is closed
with StatementFIXME "with open(...) as file" pattern
FIXME
FIXME what's a "line"? (line endings discussion, universal line endings, etc.)
FIXME
FIXME
FIXME write(), writelines(), .writeable
FIXME
FIXME
>>> image = open('examples/beauregard-100x100.jpg', 'rb')
>>> image
<io.BufferedReader object at 0x00C7A390>
>>> image.mode
'rb'
>>> image.name
'examples/beauregard-100x100.jpg'
>>> image <io.BufferedReader object at 0x00C7A390> >>> image.tell() 0 >>> data = image.read(3) >>> data b'\xff\xd8\xff' >>> image.tell() 3 >>> image.seek(0) 0 >>> data = image.read() >>> len(data) 3150
FIXME
FIXME
FIXME
© 2001–9 Mark Pilgrim