diff --git a/files.html b/files.html index 864bb27..32252dd 100644 --- a/files.html +++ b/files.html @@ -106,36 +106,35 @@ UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 28: chara
 # continued from the previous example
->>> a_file.read()    
+>>> a_file.read()                      
 ''
->>> a_file.seek(0)   
+>>> a_file.seek(0)                     
 0
->>> a_file.read(16)  
+>>> a_file.read(16)                    
 'Dive Into Python'
->>> a_file.read(1)   
+>>> a_file.read(1)                     
 ' '
->>> a_file.read(1)   
+>>> a_file.read(1)
 '是'
->>> a_file.tell()    
+>>> a_file.tell()                      
 20
    -
  1. FIXME -
  2. -
  3. -
  4. -
  5. -
  6. +
  7. Since you’re still at the end of the file, further calls to the file object’s read() method simply return an empty string. +
  8. The seek() method moves to a specific byte position in a file. +
  9. The read() method can take an optional parameter, the number of characters to read. +
  10. If you like, you can even read one character at a time. +
  11. 16 + 1 + 1 = … 20?

FIXME

 # continued from the previous example
->>> a_file.seek(17)  
+>>> a_file.seek(17)                    
 17
->>> a_file.read(1)   
+>>> a_file.read(1)                     
 '是'
->>> a_file.tell()    
+>>> a_file.tell()                      
 20
  1. FIXME @@ -206,13 +205,42 @@ ValueError: I/O operation on closed file.

    Reading Data One Line At A Time

    +

    A “line” of a text file is just what you think it is — you type a few words and press ENTER, and now you’re on a new line. A line of text is a sequence of characters delimited by… what exactly? Well, it’s complicated, because text files can use several different characters to mark the end of a line. Every operating system has its own convention. Some use a carriage return character, others use a line feed character, and some use both characters at the end of every line. + +

    Now breathe a sigh of relief, because Python handles line endings automatically by default. If you say, “I want to read this text file one line at a time,” Python will figure out which kind of line ending the text file uses and and it will all Just Work. + +

    +

    If you need fine-grained control over what’s considered a line ending, you can pass the optional newline parameter to the open() function. See the open() function documentation for all the gory details. +

    + +

    So, how do you actually do it? Read a file one line at a time, that is. It’s so simple, it’s beautiful. + +

    [download oneline.py] +

    line_number = 0
    +with open('examples/favorite-people.txt', encoding='utf-8') as a_file:  
    +    for a_line in a_file:                                               
    +        line_number += 1
    +        print('{} {}'.format(line_number, a_line.rstrip()))             
    +
      +
    1. FIXME +
    2. +
    3. +
    +

    FIXME -

    FIXME what's a "line"? (line endings discussion, universal line endings, etc.) - - +

    +you@localhost:~/diveintopython3$ python3 examples/oneline.py
    +1 Dora
    +2 Ethan
    +3 Wesley
    +4 John
    +5 Anne
    +6 Mike
    +7 Chris
    +8 Sarah
    +9 Alex
    +10 Lizzie

    Writing to Text Files