Week 7

Solutions to File I/O and Consolidatio

Exercise 1 - Basic File I/O

Here we create a file, and add some random lines to it. Then we read and print out the file contents.

week6_solutions_ex1.py
# create a file
my_file = open("musings.txt", "w+")

# write some things to the file
my_file.write("These are some of my thoughts\n")
my_file.write("The solutions are so great\n")
my_file.write("Thanks for these interesting exercises alex\n")

# close the file, so that we can then read from it
my_file.close()

# open the file for reading
my_file = open("musings.txt", "r")

# now read each line in the file, and print it out
for line in my_file:
    print(line)
    
# close access to the file again
my_file.close()

Exercise 2 - Wherefore art thou paragraph

Here we define a function to read N paragraphs from a file, and print them.

Exercise 3 - Caesar's Cipher Consolidates Cool Class

Last updated

Was this helpful?