Files

Reading

Chapter 11.

Key ideas

From chapter 11

  • Opening a file. Example: qbdata.txt.
  • Reading from a file: open(filename, "r")
  • Writing to a file: open(filename, "w")
  • Closing a file: filevariable.close()
  • File reading methods
    • filevar.readline() - in conjunction with a while loop to read an entire file
    • filevar.readlines()
  • File writing methods
    • filevar.write(some_string)
  • Absolute File Paths vs. Relative File Paths

Active learning

Activity 1

Download census.txt.

  1. Using a for loop, write a program that reports the average population across all states.
  2. Edit your existing program to use a while loop and the readline() function.

Activity 2

Edit your program from Activity 1 to write a file called census_percentages.txt that reports the percentage of the total population for each state, in the same order as census.txt. The file you write should look like this.