diff --git a/files.html b/files.html index a902827..fbb8922 100644 --- a/files.html +++ b/files.html @@ -89,32 +89,60 @@ UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 28: chara

Reading Data From A Text File

+

After you open a file for reading, you’ll probably want to read from it at some point. + +

+>>> a_file = open('examples/chinese.txt', encoding='utf-8')
+>>> a_file.read()
+'Dive Into Python 是为有经验的程序员编写的一本 Python 书。\n'
+
    +
  1. FIXME +
+

FIXME - +

  • FIXME + + +

    FIXME + +

    +>>> a_file.seek(17)
    +17
    +>>> a_file.read(1)
    +'是'
    +>>> a_file.tell()
    +20
    +
      +
    1. FIXME +
    + +

    FIXME + +

    +>>> a_file.seek(18)
    +>>> a_file.read(1)
    +Traceback (most recent call last):
    +  File "<pyshell#12>", line 1, in <module>
    +    a_file.read(1)
    +  File "C:\Python31\lib\codecs.py", line 300, in decode
    +    (result, consumed) = self._buffer_decode(data, self.errors, final)
    +UnicodeDecodeError: 'utf8' codec can't decode byte 0x98 in position 0: unexpected code byte
    +
      +
    1. FIXME +

    6.2.2. Closing Files