Albion College

CS 171

Introduction to Computer Science I & Lab

Fall 2025

Lab 7: Files

Goals and Objectives

The main goal this lab is for you to get practice reading and processing text files with Python.

Task

You will write several functions that perform some file reading and processing tasks.

Background

Files store data that persists even when the computer is restarted.

Introductions

Introduce yourself to your lab partner(s). What are your plans for fall break?

Download the lab

Download the source file Lab07.py. It contains the stubs of Python functions. Open it in the online Python environment.

Download the text file mission.txt.

Download the text file MCS-DraftSchedule-S26.tsv. It contains a csv file with tab "\t" field delimiters and one header line.

Getting Started

In this lab, you will practice writing functions that process strings. You should start off with s1 driving and s2 navigating. Complete the functions in Lab07.py. You should change driver/navigator roles after each method is completed.

Seek help as soon as you are experiencing difficulty with this assignment.
Do not wait until the deadline to seek help!

# YOUR NAMES AND EMAIL HERE # Returns a single string containing file's entire contents # parameter: filename is the name of the file to read # close the file when done def readFile(filename): return "" # should be a string # Returns a list of strings containing the lines of a file # parameter: filename is the name of the file to read def readLines(filename): return [] # should be a list of strings # print to the screen each line of the file # avoid printing extra characters, such as double new lines def printFile(filename): return None # don't return anything # find the longest "word" (including punctuation) in the input file # hint: use split() def longestWord(filename): return "" # should be a string that is the longest word # print to the screen # the number of chracters, words, and lines # here a word is any whitespace delimited text # hint: use split(), len() def counts(filename): return None # don't return anything #for each character in the file, include whitespace # output on a single line with a space between fields # the index of each character (4 digits) # the character # the character's ordinal value in decimal (3 digits) # the character's ordinal value in hexadecimal (2 hexdigits) # the character's ordinal value in binary (8 bits) # formatted as indicated above, for example: # 4 A 65 0x41 0b01000001 # # hint: use ord(), hex(), bin() # See https://docs.python.org/3/library/string.html for formatted output def dump(filename): return None # don't return anything # Returns a list of lines of a file # with each line containing a list # parameter: filename is the name of the file to read # parameter: sep is the character that separates fields # parameter: skip is the number of header lines to skip def readCSV(filename, sep, skip): return None # should be a list containing lists of strings # print to the screen the entries in the file # See MCS-DraftSchedule-S26.tsv def printSchedule(filename, subject): return None # don't return anything # print to the screen the file contents formatted so that # at least one word is printed, # then print at most n characters per line (including spaces) # lines should not start with whitespace def formatFile(filename, n): return None # don't return anything def main(): text = readFile("mission.txt") print(len(text)) lines = readLines("mission.txt") print(len(lines)) lines = readLines("Lab07.py") print(len(lines)) printFile("mission.txt") longest = longestWord("mission.txt") print(longest) counts("mission.txt") dump("mission.txt") printSchedule("MCS-DraftSchedule-S26.tsv", "CS") # CS courses printSchedule("MCS-DraftSchedule-S26.tsv", "MATH") # MATH courses printSchedule("MCS-DraftSchedule-S26.tsv", "") # All courses formatFile("mission.txt", 80) # at most 80 characters per line formatFile("mission.txt", 40) # at most 40 characters per line formatFile("mission.txt", 1) # one word per line main()

Have Fun!

Final Steps

When done, email you lab partner and instructor your lab (.py file).