T1W6 Files & Modules - tstorrnetnz/teaching2025 GitHub Wiki

Files

Reading files and writing to files is essential for most programs - it is how data is stored and retrieved. Fortunately Python has quite a few functions to help you do this and are mostly simple to use.

For you to do:

  1. The chapter in Learn Python The Right Way that covers files, Chapter 13 is the simplest most readable introduction that I have seen - please read it all. It also includes accessing files from the internet.

Modules

You should now be (more or less) familiar with the import statement that we sometimes use at the start of a program e.g. [Chapter 120(https://learnpythontherightway.com/chapter/chapter-12.html) has a great overview of this. Read through it before you attempt the problems below.

import string

ss = "Hello World! "

tt = ss.swapcase()

print(tt)

This enables us to use some string methods (for example swapcase) that are not loaded by default. What you are actually doing is importing the string module.

You can import lots of pre-made modules or even build one yourself. In our case, making a test module might be quite useful so that we cam import it wherever we need rather than having to copy the code.

For You To Do:

  1. Complete the problems for chapters 12 and 13 below

Chapter 12 exercises:

  1. Open help for the calendar module.

    1. Try the following:

      import calendar
      cal = calendar.TextCalendar()      # Create an instance
      cal.pryear(2012)                   # What happens here?
      
    2. Observe that the week starts on Monday. An adventurous CompSci student believes that it is better mental chunking to have his week start on Thursday, because then there are only two working days to the weekend, and every week has a break in the middle. Read the documentation for TextCalendar, and see how you can help him print a calendar that suits his needs.

    3. Find a function to print just the month in which your birthday occurs this year.

    4. Try this:

      d = calendar.LocaleTextCalendar(6, "SPANISH")     
      d.pryear(2012)   
      
      

      Try a few other languages, including one that doesn't work, and see what happens.

    5. Experiment with calendar.isleap. What does it expect as an argument? What does it return as a result? What kind of a function is this?

      Make detailed notes about what you learned from these exercises.

    6. Open help for the math module.

    How many functions are in the math module? What does math.ceil do? What about math.floor? (hint: both floor and ceil expect floating point arguments.) Describe how we have been computing the same value as math.sqrt without using the math module. What are the two data constants in the math module?

Chapter 13 exercises:

  1. Write a program that reads a file and writes out a new file with the lines in reversed order (i.e. the first line in the old file becomes the last one in the new file.)
  2. Write a program that reads a file and prints only those lines that contain the substring snake.
  3. Write a program that reads a text file and produces an output file which is a copy of the file, except the first five columns of each line contain a four digit line number, followed by a space. Start numbering the first line in the output file at 1. Ensure that every line number is formatted to the same width in the output file. Use one of your Python programs as test data for this exercise: your output should be a printed and numbered listing of the Python program.
  4. Write a program that undoes the numbering of the previous exercise: it should read a file with numbered lines and produce another file without line numbers.