10READING AND WRITING FILES

Your Python programs can directly interact with the contents of text files. By saving and opening files, they can store the data they work with, then continue where they left off the next time you run them. Reading data from files on the hard drive also lets your programs process data from other applications.

A simple drawing of a light bulb. LEARNING OBJECTIVES

  • Understand the filesystem, including how filenames and paths serve as addresses for files.
  • Know how Python represents filepaths, both as strings and using the pathlib module.
  • Read and write text files using Path methods and the open() function.
  • Be able to save Python data structures using shelf files and the shelve module.

A grey circle with a white question mark at the center Practice Questions

These questions test your understanding of how computer files and file-systems work, as well as how to read and write text data to these files.

Files and Filepaths

A file has two key properties: a filename (usually written as one word) and a path. The path specifies the location of a file on the computer. Your computer’s filesystem begins with a root folder that contains all other files and subfolders. Python’s pathlib module and Path objects represent filepaths and provide several methods for manipulating them.

  1. 1. What is another term for folder?

  2. 2. What character separates folders and filenames on Windows?

  3. 3. What character separates folders and filenames on macOS and Linux?

  4. 4. What is a root folder?

  5. 5. What does an absolute filepath begin with?

  6. 6. What is a relative filepath relative to?

  7. 7. What common import statement lets your Python code use Path objects?

For questions 8 through 10, determine what the expression evaluates to, then note whether it’s a relative or an absolute path.

  1. 8. Path('spam', 'bacon', 'eggs')

  2. 9. Path('spam') / Path('bacon') / Path('eggs')

  3. 10. Path('spam') / 'bacon' / 'eggs'

  4. 11. Does 'spam' / 'bacon' / 'eggs' evaluate to a Path object?

  5. 12. If the current working directory is Path(r'C:\spam'), what does Path('eggs.txt') refer to?

  6. 13. Which function changes the Python program’s current working directory?

  7. 14. If the current working directory is Path(r'C:\spam'), what does Path(r'..\eggs.txt') refer to?

  8. 15. If the current working directory is Path(r'C:\spam'), what does Path.cwd() refer to?

  9. 16. What is the parent folder of C:\spam\eggs.txt?

  10. 17. Write the code that gets a Path() object of the parent of the current working directory.

  11. 18. What do the st_atime, st_ctime, and st_mtime attributes of stat_result objects returned by the stat() method represent?

  12. 19. What does * mean in a glob pattern?

  13. 20. What does ? mean in a glob pattern?

  14. 21. What do the True and False returned by the exists() method mean?

  15. 22. If the path represented by a Path object doesn’t exist, what do the is_file() and is_dir() methods return?

The File Reading and Writing Process

Plaintext files contain only basic text characters and don’t include font, size, or color information. The pathlib module’s read_text() method returns the full contents of a text file as a string. Its write_text() method creates a new text file (or overwrites an existing one) with the string passed to it. Answer the following questions about reading and writing strings with plaintext files.

  1. 23. Does the text in a plaintext file have font, size, and/or color information?

  2. 24. Are PDFs and spreadsheet files examples of plaintext files or binary files?

  3. 25. What code returns a string of the Path('eggs.txt') object’s plaintext file contents?

  4. 26. If the eggs.txt file already contains the plaintext 'Hello', what does it contain after running Path('eggs.txt').write_text('Goodbye')?

  5. 27. What encoding do you almost certainly want to use when reading and writing plaintext files?

  6. 28. What mode does the code open('eggs.txt', encoding='utf-8') open the eggs.txt file in?

  7. 29. After running file_obj = open('eggs.txt', encoding='utf-8'), how do you get the plaintext contents of eggs.txt as a single string?

  8. 30. And how do you get the plaintext contents of eggs.txt as a list of strings (one string per line)?

  9. 31. The variable contents contains a string. What code would write this string to a file named eggs.txt using the write_text() method?

  10. 32. And what code would write this string to a file named eggs.txt? (Do not use the write_text() method.)

  11. 33. A context manager is created by what kind of statement?

  12. 34. What is the benefit of using a context manager with the open() function instead of the open() function and close() method?

Saving Variables with the shelve Module

You can save variables in your Python programs to binary shelf files using the shelve module. Doing so lets your program restore that data to the variables the next time it is run. You can make changes to the shelf value as if it were a dictionary. Answer the following questions about the shelve module and shelf files.

  1. 35. When calling shelve.open() to open a shelf file, do you need to specify the file extension?

  2. 36. What Python data structure is the shelf file similar to?

  3. 37. What methods can you call on a shelf object to get its keys and values?

A simple drawing of a sharpened pencil. Practice Projects

For more practice manipulating files, try the following short projects.

Text File Combiner

Let’s create a function named combine_two_text_files() that can combine the contents of two text files. The function takes three arguments: two filenames of text files whose contents should be read, and the filename of a third text file in which to write the combined contents.

For example, if your function were named combine_two_text_files(), calling combine_two_text_files('spam.txt', 'eggs.txt', 'output.txt') would create a new file named output.txt with the contents of the spam.txt and eggs.txt files.

Save the function in a file named textFileCombiner.py.

Zigzag File

The Zigzag program from Chapter 4 of Automate the Boring Stuff with Python prints a pattern like the following:

********
 ********
  ********
   ********
    ********
     ********
      ********
     ********
    ********
   ********
  ********
 ********
********
 ********
  ********
   ********
    ********

Your friend sees this and decides they’d like to make it their (very long) email signature or save it for some other use. Re-create this program, except add a function named write_zigzag() that writes the zigzag text to a file named zigzag.txt instead of printing it to the screen. This way, you can email the text file to your friend so that they can store it on their computer.

While the screen version goes on forever, your program should write only 1,000 lines of zigs and zags to the file. Remember to remove the time.sleep() call from the original Zigzag program, as you won’t need it for this project.

Save this program in a file named zigZagFile.py.

Rock, Paper, Scissors with Saved Games

The rock, paper, scissors game in Chapter 3 of Automate the Boring Stuff with Python records how many wins, losses, and ties the player has. But these stats are tracked only while the program is running. Using the shelve module, add the ability to save these stats and load them the next time the program runs.

Note that if the program has never previously saved the game stats, the wins, losses, and ties should all default to 0. Otherwise, the program should load these numbers when it starts and update them after each game.

Save this program in a file named rpsSaved.py.