bembry.org
Home / Technology / Python / Exercises

Python Lesson 12 Exercises: File Manipulation

In the following exercise, pay attention to your user interface, and be certain to test the programs rigorously. Save each program as pa11-1.py, pa11-2.py and so forth.

Exercise 1:
Modify the password generator program (pa10-3) so that it can create multiple passwords (as many as the user wants) and save them in a text file.

Exercise 2:
Write a program that will search recursively through a directory to find all the files with a particular extension and then rename those files. The program should ask the user what file extension to look for and what the new file extension should be.
Example: Program will look for all files saved as ".html" and change them to ".shtml".

Exercise 3:
When maintaining a web site it can become difficult to remember which pages have code that needs updating. So, write a program that will go through all the files in a directory (and all the files in the subdirectories) and replace the old code with new code entered by the user. The user should enter what the old text is and what the new text is.
Example: Program will search for all references to "http://www.mhafyos.org" and change them to "http://www.bembry.org".

Exercise 4:
Write a program that will take a text file as input, either encrypt or decipher the text using a keyword cipher (Vigenere cipher), and save the modified text file. The program should ask the user for the keyword to use in the cipher, whether the file needs to be encrypted or deciphered, and the name of both the input file and the output file.

In a Vigenere cipher, each letter of the alphabet maps to a number. For our program we will use the assigned ASCII value of each character. To encrypt the message, a special key word is used. The keyword is repeated as often as necessary, and the values of the letters in the keyword are added to the values of the letters in the cypher text to create the encrypted message. When it is time to decipher the message, the value of the the letters in the keyword are subtracted from the values of the the letters in the encrypted message and the result is the original text. Make sense? Of course not. So, here is an example of how to encrypt a message using the Vigenere ciphere. I'll leave it to you to figure out how to decipher it.

Example:

Message: welcome home
Keyword: python
Cipher text: ç Þ à Ë Þ Û Õ ™ Ü × Ü Ó

To encrypt the text, we take the word "python" and make it at least the same size as "welcome home" by repeating it as follows:
w e l c o m e   h o m e
p y t h o n p y t h o n

Then, we convert each letter into its numerical ASCII value as follows:
w e l c o m e   h o m e = 119 101 108 099 111 109 101 032 104 111 109 101
p y t h o n p y t h o n = 112 121 116 104 111 110 112 121 116 104 111 110

Next, we add the numbers together.
  119 101 108 099 111 109 101 032 104 111 109 101
+ 112 121 116 104 111 110 112 121 116 104 111 110
--------------------------------------------------
  231 222 224 203 222 219 213 153 220 215 220 211

We do need to be aware that any number larger than 255, or smaller than 1, will not have an appropriate ASCII code and thus cause an error. All of these values are within range, however, and we do not need to make any corrections

And, finally, we convert the numbers back into their corresponding ASCII character:
231 222 224 203 222 219 213 153 220 215 220 211 = ç Þ à Ë Þ Û Õ ™ Ü × Ü Ó

To do this assignment, you will need to use the builtin functions ord() and chr(). ord() takes a letter and returns the numerical ASCII value of that letter. chr() takes a number and returns the related ASCII letter for that value.

Alternative 1:
Create a random poetry generator. The program will ask the user how many lines the poem should be, and how many words should be on each line. Then, the program will read in text from a text file (you may use the dictionary file or create your own text files) and create a "poem". The poem should be displayed on the screen, and the user should have the choice of whether to save the poem to a text file, to run the program again, or to exit.

Alternative 2:
Create a Mad-Lib program. The program will ask the user for different parts of speech (nouns, verbs, adjectives, etc.), then open a text file containing a mad-lib story, insert the nouns and verbs in appropriate places, display the result, then give the user a chance to either save the result or exit.

Lesson Exercises Python Lessons Homepage

Restricted access