CS 115 Lab 10, Part A: Read an input file

[Back to lab instructions]


Instructions

  1. Create a new Python source code file named lab10a.py:
    """
    Program: CS 115 Lab 10
    Author: Your name
    Description: This program will open a file and then search its contents
                 using linear and binary search.
    """
    
    
    def readfile(filename):
        """
        Reads the entire contents of a file into a single string using
        the read() method.
    
        Parameter: the name of the file to read (as a string)
        Returns: the text in the file as a large, possibly multi-line, string
        """
    
        infile = open(filename, "r")  # open file for reading
    
        # Use Python's file read function to read the file contents
        filetext = infile.read()
    
        infile.close()  # close the file
    
        return filetext  # the text of the file, as a single string
    
    
    def main():
        """ Read and print a file's contents. """
        pass
    
    
    main()
    
  2. Read this code carefully before continuing and answer Question 4.
  3. Right now, this code doesn't do anything because the definition of main is empty. To fill out the definition of main, replace the pass with the following 3 lines of code:
  4. Run your program. Type cities.txt when prompted. Verify that your program prints the names of cities.
  5. Answer Question 5 in your writeup. To help you answer these questions, you can print the length of the information you read from the file -- just comment out any extra print statements when you are finished.
  6. Inside the definition of readfile, add a call to split() at the end of the call to read:
    filetext = infile.read().split()
    Answer Question 6 in your writeup.
  7. To split the text from the file into a list of lines rather than a list of words, modify your line of code as shown:
    filetext = infile.read().splitlines()
    Answer Question 7 in your writeup.
  8. Here are some samples of what your program should be doing at this point. Modify your wording to match the example, and add code to print the number of lines in the file if you do not have it already.

    Sample 1

    Name of input file: cities-small.txt
    Number of lines in file: 5
    ['Santa Rosa', 'Petaluma', 'Rohnert Park', 'Windsor', 'Healdsburg']

    Sample 2

    Name of input file: cities.txt
    Number of lines in file: 50
    ['Los Angeles', 'San Diego', 'San Jose', 'San Francisco', 'Fresno', 'Sacramento', 'Long Beach', 'Oakland', 'Bakersfield',
    'Anaheim', 'Santa Ana', 'Riverside', 'Stockton', 'Chula Vista', 'Fremont', 'Irvine', 'San Bernardino', 'Modesto', 'Oxnard', 
    Fontana', 'Moreno Valley', 'Glendale', 'Huntington Beach', 'Santa Clarita', 'Garden Grove', 'Santa Rosa', 'Oceanside', 'Rancho
    Cucamonga', 'Ontario', 'Lancaster', 'Elk Grove', 'Palmdale', 'Corona', 'Salinas', 'Pomona', 'Torrance', 'Hayward', 
    Escondido', 'Sunnyvale', 'Pasadena', 'Orange', 'Fullerton', 'Thousand Oaks', 'Visalia', 'Simi Valley', 'Concord', 'Roseville', 
    Santa Clara', 'Vallejo', 'Victorville']
  9. Add the following function definition to your program:
    def print_list(list_to_print):
        """
        Print the contents of a list.
    
        Parameter: the list to print
        Returns: nothing
        """
    
        for i, item in enumerate(list_to_print):
            print(i, ': ', item, sep="")
  10. Modify your main function to call your print_list function instead of printing the list directly. Sample output:
    Name of input file: cities.txt
    Number of lines in file: 50
    0: Los Angeles
    1: San Diego
    2: San Jose
    3: San Francisco
    [etc]
  11. Answer Question 8 in your writeup.
  12. Add code at the end of your main function to prompt the user for the names of cities until the user types quit:
    Name of input file: cities.txt
    Number of lines in file: 50
    0: Los Angeles
    1: San Diego
    2: San Jose
    3: San Francisco
    [etc]
    
    Enter the name of a city: Cotati
    
    Enter the name of a city: Fresno
    
    Enter the name of a city: Santa Rosa
    
    Enter the name of a city: quit
    
  13. Continue to Part B.