Concepts / Reading Data from Files: The read() and readline() Methods

Reading Data from Files: The read() and readline() Methods

The write() method puts data into a file exactly as provided and returns the character count written.

  • Programming

What Reaches the File

A file receives exactly the data supplied to write(). The method does not silently add spaces, line breaks, or other characters. This means that the strings you pass to consecutive write() calls directly determine the sequence of characters stored in the file.

Tracing Consecutive Writes

Consecutive write() calls place their data sequentially from the current file position. After one call inserts its characters, the next call begins immediately after them. If the first string ends without a newline, the next string begins on the same line. If the first string includes a newline, the next string begins after that line ending.

thenthenproduceswrite("Red")characters: RedRedBlue\nGreenfinal file contentwrite("Blue\n")characters: Blue andnewlinewrite("Green")characters: Green
What file contents result when each write() call contributes its characters in sequence?

Following three writes

Predict the resulting file content after these calls: write("Red"), write("Blue\n"), and write("Green").

First call: The first call inserts Red. Because it contains no newline, the file position is immediately after the d.

Second call: The second call inserts Blue immediately after Red and then inserts a newline character.

Third call: The third call begins after the newline, so Green starts on the next line.

The file contains RedBlue on the first line and Green on the next line.

Counting Inserted Characters

The write() method returns the number of characters written. The count applies to the data supplied in that call, including characters such as spaces and the newline character included through \n.

write() insertswrite() returns"Hi\n"three charactersHi\nthree characters inserted3returned count
How does the integer returned by write() relate to the characters supplied to the method?
python
Output
count is 3 because the supplied data contains H, i, and the newline character.

Controlling Line Endings

write() does not automatically add a newline in the way print() does. To move later data to a new line, include \n in the string passed to write(). The position of that newline matters: a newline at the end of a string moves the next write() call to the following line, while a string without a newline leaves the next call directly beside its characters.

followed by write("B")followed by print("B")write("A")no automatic newlineABtwo calls share a lineprint("A")moves to a new lineA then Blater output begins on anew line
Why do write() and print() place later text differently when no explicit newline is supplied to write()?

Decide where each line should end before writing the strings. Add \n explicitly at the end of strings that should be followed by a new line. This makes the intended file layout visible in the data being written.

Mistakes with File Layout

  • Assuming write() adds a newline automatically

    write() inserts the supplied data exactly as provided and does not automatically add a newline.

    Fix: Use file.write("First\n") when the second write should begin on a new line.

  • Forgetting that consecutive calls are sequential

    Each call begins at the current file position, so the characters are placed directly beside the preceding characters.

    Fix: Predict the file as ABC, or include newline characters where separate lines are required.

  • Miscounting the return value

    The returned count represents the characters supplied, including the newline character.

    Fix: Count H, i, and the newline as three written characters.

A string can contain a newline without placing that newline at its end. Because write() uses the data exactly as provided, the newline appears at that precise position and subsequent characters in the same string continue after it.

Practice the Trace

What do you think happens?

A file starts empty. What content results from these calls in order: write("One\n"), write("Two"), and write("\nThree")?

  • OneTwoThree on one line
  • One on the first line, Two on the second line, and Three on the third line
  • One on the first line, TwoThree on the second line
  • OneTwo on the first line and Three on the second line
Reveal answer

Answer: One on the first line, Two on the second line, and Three on the third line

The first string ends with a newline, so Two begins on the next line. The third string begins with a newline, so it first ends the line containing Two; Three then begins on the following line.

EASY

Predict both the file contents and the return value for a call that writes the string "Ready\n". Then explain which character causes the next write() call to begin on a new line.

Hints
  • Count every character in the supplied string.
  • Treat \n as an explicitly supplied newline character.
  • The newline is both part of the written data and the reason later data starts on the following line.

Key Takeaways

  1. write() inserts data exactly as supplied.
  2. The integer returned by write() is the number of characters written.
  3. write() does not automatically add a newline like print() does.
  4. Consecutive write() calls continue from the current file position.
  5. Include \n explicitly wherever the file needs a line ending.

Key Takeaways

  • write() places the supplied characters into the file without automatically changing them.
  • The return value reports how many characters that call wrote.
  • Newline characters must be included explicitly when separate lines are required.
  • The final file layout can be predicted by tracing each write() call in order.