liveBook · Manning

Read_file

As explained in Get Programming: Learn to code with Python

read_file is a book-specific function that reads friend names and phone numbers from a file and returns them as grouped tuples.

read_file reads a plaintext file line by line, removes each line’s newline character, and groups alternating lines into names and phone-number tuples. section 23.1

The function expects a simple repeating layout:

  • Put a friend’s name on one line and that friend’s phone number on the next. section 23.1
  • Store names from every other line in one tuple and phone numbers from the remaining lines in another. section 23.1
  • Return the two tuples together as a tuple of tuples, with matching indexes identifying each friend. section 23.1

Reading and checking friends.txt

Open the file, pass its file object to read_file, unpack the returned names and phones, print them, and close the file. A sample file contains Bob, a phone number, Mom Bob, another phone number, and so on. section 23.3

Listing 23.2. Read names and phone numbers from a file
def read_file(file):
    """                                                           #1
    file, a file object                                           #1
    Starting from the first line, it reads every 2 lines          #1
    and stores them in a tuple.                                   #1
    Starting from the second line, it reads every 2 lines         #1
    and stores them in a tuple.                                   #1
    Returns a tuple of the two tuples.                            #1
    """                                                           #1
    first_every_2 = ()                                            #2
    second_every_2 = ()                                           #2
    line_count = 0                                                #3
    for line in file:                                             #4
        stripped_line = line.replace("\n", "")                    #5
        if line_count%2 == 0:                                     #6
            first_every_2 += (stripped_line,)                     #7
        elif line_count%2 == 1:                                   #8
            second_every_2 += (stripped_line,)                    #9
        line_count += 1                                           #10
    return (first_every_2, second_every_2)                        #11

The function listing shows the setup for two result tuples, a line counter, and iteration through the file. section 23.1

Listing 23.4. Read names and phone numbers from a file
friends_file = open('friends.txt')                    #1
(names, phones) = read_file(friends_file)             #2

print(names)                                          #3
print(phones)                                         #3
clean_phones = sanitize(phones)                       #4

print(clean_phones)                                   #5
friends_file.close()                                  #6

The surrounding usage example shows opening friends.txt, calling read_file, unpacking its result, and inspecting the returned tuples. section 23.3

How Get Programming: Learn to code with Python covers it

How deep it goes
Explains the input layout, newline removal, alternating-line grouping, and file-object call, but stops before the later analysis.
Examples
Uses friends.txt with alternating names and phone numbers, plus small newline-removal demonstrations.
What it assumes
Assumes basic Python functions, loops, strings, tuples, and file objects while explaining newline handling directly.
What you can do after
Apply it via Python to load alternating contact records and inspect the resulting names and phone-number tuples.
How it connects
Connects file objects, loops, newline characters, string methods, tuples, and function testing.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming: Learn to code with Python. The section links point to the parts of the book it draws on.