Lab 11: File I/O
Program 1: Word Counter
word_counter.py
'''
Author name: <Your name here>
This program will count the words in a text file.
'''
# Open the file for reading.
with open("sample.txt", "r") as file:
# Initialize a word counter.
word_count = 0
# For each line in a file.
for line in file:
# Increment the word count by the length of the line
# after it is split into a words list by spaces.
word_count += len(line.split())
# Print out the word count.
print(word_count)
OR:
'''
Author name: <Your name here>
This program will count the words in a text file.
'''
print(len(open("sample.txt", "r").read().split()))
Program 2: Uppercase Writer
uppercase_writer.py*
'''
Author name: <Your name here>
This program will open a file and
write the contents as all upper case letters.
'''
# A list of lines to write
lines_to_write = []
# With the file to read open. Using read mode.
with open("textfile.txt", "r") as file_to_read:
# For each line in the file.
for line in file_to_read:
# Append the upper case version of the line to the lines_to_write list.
lines_to_write.append(line.upper())
# With te file to write open. Using write mode.
with open("uppercase_output.txt", "w") as file_to_write:
# Write the lines_to_write list to the file.
file_to_write.writelines(lines_to_write)
OR:
'''
Author name: <Your name here>
This program will open a file and
write the contents as all upper case letters.
'''
# With the file to read open. Using read mode.
with open("textfile.txt", "r") as file_to_read:
# Read the text into a file.
text = file_to_read.read()
# With te file to write open. Using write mode.
with open("uppercase_output.txt", "w") as file_to_write:
# Write the text uppercase to a file.
file_to_write.write(text.upper())
Program 3: Grade Average Calculator
grade_calculator.py
'''
Author name: <Your name here>
This program will read a list of grades from a file,
calculates the average grade, and appends the average
to the end of the file.
'''
# With the file grades.txt open in read/write mode.
with open("grades.txt", "r+") as grades_file:
# Create a new grades list.
grades = []
# For each line in the grades file.
for line in grades_file:
print(line)
# Add the grade to the grades list after converting to a number.
grades.append(eval(line))
# Compute the average.
average = sum(grades) / len(grades)
# Now append the average to the file.
grades_file.write("\nAverage: " + str(average))
Program 4: Student Grades Report
student_grades_report.py
'''
Author name: <Your name here>
This program will read student grades from a file,
processes each student’s grades, and writes a report
with the highest, lowest, and average grades for each
student to a new file.
'''
# Open the student grades file.
# The file is of the format: StudentName, Grade1, Grade2, Grade3
with open("student_grades.txt", "r") as grades_file:
with open("grades_report.txt", "w") as grades_report:
# For each line in the student grades file.
for line in grades_file:
# Split the line into a list. This split will occur at the commas.
grade_file_line_as_list = line.split(",")
# Get the student, which is the first item in the list (index 0).
student = grade_file_line_as_list[0]
# Get the grades which are the remaining items.
# Use a list splice to get from index 1 until the end.
grades_as_strings = grade_file_line_as_list[1:]
# Create a new empty list to store the grades after we convert them to integers.
grades_as_integers = []
# Convert the grades as strings list to grades as integers list.
# For each grade in the grades as strings list.
for grade in grades_as_strings:
# Evaluate the grade to convert it to a number.
grades_as_integers.append(eval(grade))
# The output format is: StudentName - Highest: 90, Lowest: 79, Average: 85.67
# Build the report string, end it with a new line character \n.
report = student \
+ " - Highest: " + str(max(grades_as_integers)) \
+ ", Lowest: " + str(min(grades_as_integers)) \
+ ", Average: " + str(sum(grades_as_integers) / len(grades_as_integers)) + "\n"
# Write this report string to the grades report file.
grades_report.write(report)