""" 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()
filetext = infile.read().split()Answer Question 6 in your writeup.
filetext = infile.read().splitlines()Answer Question 7 in your writeup.
Name of input file: cities-small.txt
Number of lines in file: 5
['Santa Rosa', 'Petaluma', 'Rohnert Park', 'Windsor', 'Healdsburg']
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']
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="")
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]
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