← Back to course
Code Crew G6+ / Week 08 / Tuesday
2/6
Week 08 Β· Files

Tuesday

Reading a file
// Saving work that lasts
⏱ about 30 min

Tuesday: Reading a file

crew.txt sits in the mission folder, saved and safe. Now Rocket wants a different program to read it and greet each crew member by name.

His first try prints the names with a blank line after every one. "Why is it double spaced?" he asks. "I did not add any extra lines."

Raven looks at the file and then at the print. "What do you notice?" she asks. "Each line in the file already ends with a newline. Then print adds one more."

Nova highlights the invisible newline at the end of each line. "Interesting," she says. "You met a tool in week 3 that removes spaces from the ends of a string. It removes newlines too."

Rocket adds .strip() and the spacing snaps into place.

Reading the whole file

open("crew.txt") with no mode opens the file for reading. That is the same as mode "r". Inside the with block, f.read() gives the whole file as one string.

# read_all.py
with open("crew.txt") as f:
    text = f.read()
print(text)
print(len(text), "characters")
Rocket
Raven
Nova

18 characters
Why does read_all.py show 18 characters for 15 letters?
WHY THIS EXERCISEEach line ends with a newline character, so 15 letters plus 3 newlines make 18 characters.

Reading line by line

A for loop over the file gives one line at a time. Each line still ends with its newline, so use .strip() to remove it.

# greet_crew.py
with open("crew.txt") as f:
    for line in f:
        name = line.strip()
        print("Welcome aboard,", name)
Welcome aboard, Rocket
Welcome aboard, Raven
Welcome aboard, Nova
READING FILES
  • Read the question.
  • Tap your answer.
You forget .strip() in greet_crew.py. What changes?

FileNotFoundError

Opening a file that does not exist, for reading, gives a FileNotFoundError. Check the spelling of the name, and check that the file is in the same folder.

# missing.py
with open("crew_list.txt") as f:
    print(f.read())
Traceback (most recent call last):
  File "missing.py", line 2, in <module>
    with open("crew_list.txt") as f:
         ~~~~^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'crew_list.txt'
The file is called crew.txt. What is wrong in missing.py? Give the name it should open.
WHY THIS EXERCISEThe program asked for crew_list.txt, which does not exist. The real file is crew.txt.
At your computer
1. Make sure crew.txt from Monday is in your mission folder.
2. Type read_all.py, save it and run it. Check for 18 characters.
3. Type greet_crew.py, save it and run it.
4. Delete .strip() from greet_crew.py and run it. Then put it back.
5. Type missing.py and run it. Read the error, then fix the file name.
PRACTICE: READING FILES
  • Read the question.
  • Tap your answer.
crew.txt has 3 lines. How many times does for line in f: go round?
What does "Nova\n".strip() give?
Which line opens notes.txt for reading?
You open a file that is not there, for reading. Which error appears?
A file holds ab, then cd, on two lines. How many characters does f.read() give?
What does len("Raven\n".strip()) give?
What mode letter means read?

Now your programs can write and read. Tomorrow you will add to a file without wiping out what is already there.

← Monday