Starting from:

$30

CSC401–Introduction to Programming 5 Solved

In this assignment, you will modify the solution to Assignment 4 (encrypter/decrypter) by (1) adding exception handling, by (2) moving encrypter/decrypter function module to a different folder, and by (3) adding a check to see if the main section of code is being run as “__main__”.  

Start with a copy of your original encrypter/decrypter assignment python file.  We will not be using the top_word function so that can be removed. 

(If your Assignment 4 submission is not usable for this assignment, you may use the provided sample solution for Assignment 4 to do this assignment). 

1)      First, modify the encrypt_file function so that it returns True at the end of its processing. 

2)      Add file exception handling as follows: 

a.      Modify the “encrypt_file” function so that if opening the text input file name passed in results in a FileNotFoundError, the function returns None. Otherwise the function should work as normal. 

b.      Modify the “encrypt_file” function again, so that if opening either output file (the “.enc” or the “.dict” file) fails due to a PermissionError, the function returns None. Otherwise the function should work as normal. 

c.       Modify the “decrypt_file” function so that if using the file name passed in results in a 

FileNotFoundError, the function returns None. Otherwise the function should work as normal. 

d.      Modify the “decrypt_file” function again, so that if opening the output file (the “.dec” file) fails due to a PermissionError, the function returns None. Otherwise the function should work as normal. 

e.      Test your functions. You can test for file not found errors by using incorrect filenames, and for permission errors by setting the file to be overwritten to read-only. 

3)      Create a new python file: “main.py”. Move the main code from your original python file into this new python file. If done correctly, the main code will be in main.py file, and only functions will remain in the original python file 

4)      Next, move (do not copy – move) the python file that contains your functions to a different folder. Then in the “main.py” file, add that folder path to the module import path, and add the appropriate import statement(s) to import the 2 functions. 

Test your program by running main.py – it should work as normal. 

5)      Modify your code in “main.py to only call the decrypt_file function if the encrypt_file function does not return None 

 

 

                                                                        Programming 
6)      Now, modify your “main.py” by moving your main code into an “if” block that checks to insure that “__name__” is equal to “__main__”. 

7)      Modify your function file by adding an “if” block that checks if  “__name__” is equal to  “__main__”. 

Inside that IF block, add one print statement that says, “Run the program using ‘main.py’” 

To test, first run the “main.py” file to execute the program – it should execute normally. Then try to execute the function file. You should see the message Run the program using ‘main.py’ 

More products