pprint.pprint()
function will “pretty print” the contents of a list or dictionary to the screen, while the pprint.pformat()
function will return this same text as a string instead of printing it. Not only is this string formatted to be easy to read, but it is also syntactically correct Python code. Say you have a dictionary stored in a variable and you want to save this variable and its contents for future use. Using pprint.pformat()
will give you a string that you can write to .py file. This file will be your very own module that you can import whenever you want to use the variable stored in it.For example, enter the following into the interactive shell:
that stored the passwords in a dictionary. Updating the passwords required changing the source code of the program. This isn’t ideal because average users don’t feel comfortable changing source code to update their software. Also, every time you modify the source code to a program, you run the risk of accidentally introducing new bugs. By storing the data for a program in a different place than the code, you can make your programs easier for others to use and more resistant to bugs.Files are organized into folders (also called directories), and a path describes the location of a file. Every program running on your computer has a current working directory, which allows you to specify file paths relative to the current location instead of always typing the full (or absolute) path. The os.path
module has many functions for manipulating file paths.
Your programs can also directly interact with the contents of text files. The open()
function can open these files to read in their contents as one large string (with the read()
method) or as a list of strings (with the readlines()
method). The open()
function can open files in write or append mode to create new text files or add to existing text files, respectively.
In previous chapters, you used the clipboard as a way of getting large amounts of text into a program, rather than typing it all in. Now you can have your programs read files directly from the hard drive, which is a big improvement, since files are much less volatile than the clipboard.
In the next chapter, you will learn how to handle the files themselves, by copying them, deleting them, renaming them, moving them, and more.
For practice, design and write the following programs.
Extend the multiclipboard program in this chapter so that it has a delete <keyword>
command line argument that will delete a keyword from the shelf. Then add a delete
command line argument that will delete all keywords.
Create a Mad Libs program that reads in text files and lets the user add their own text anywhere the word ADJECTIVE, NOUN, ADVERB, or VERB appears in the text file. For example, a text file may look like this:
The ADJECTIVE panda walked to the NOUN and then VERB. A nearby NOUN was unaffected by these events.
The program would find these occurrences and prompt the user to replace them.
Enter an adjective: silly Enter a noun: chandelier Enter a verb: screamed Enter a noun: pickup truck
The following text file would then be created:
The silly panda walked to the chandelier and then screamed. A nearby pickup truck was unaffected by these events.
The results should be printed to the screen and saved to a new text file.