0% found this document useful (0 votes)
2 views

Programming Assignment Unit 8

This programming assignment focuses on file handling in Python, specifically reading a dictionary from a file, inverting its key-value pairs, and writing the modified dictionary to another file. It emphasizes the importance of exception handling to prevent program crashes due to file-related errors. The assignment aims to enhance data organization and retrieval efficiency while ensuring robust error management.

Uploaded by

ashigull487
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Programming Assignment Unit 8

This programming assignment focuses on file handling in Python, specifically reading a dictionary from a file, inverting its key-value pairs, and writing the modified dictionary to another file. It emphasizes the importance of exception handling to prevent program crashes due to file-related errors. The assignment aims to enhance data organization and retrieval efficiency while ensuring robust error management.

Uploaded by

ashigull487
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1

Programming Assignment Unit 8

Ayesha Gul

University of the People

CS 1101: Programming Fundamentals

Dr Josiah Jolaoluwa

26th of March, 2025


2
Programming Assignment Unit 8

File handling in Python allows data to be stored persistently, enabling long-term data

management beyond the program's runtime. This assignment demonstrates the process of reading

a dictionary from a file, processing it to invert the key-value pairs, handling exceptions, and

writing the modified dictionary to another file. Exception handling ensures that file-related

errors, such as missing files or incorrect formatting, do not crash the program. By implementing

this approach, we enhance data organization, improve retrieval efficiency, and ensure robust

error handling for smooth execution.

Input:
3
Programming Assignment Unit 8

Output:

Explanation:

1.​ Reading from a File:

a. The function read_dictionary_from_file opens and reads the input file.

b. ast.literal_eval safely converts the text into a dictionary.

c. Exception handling ensures the program does not crash due to missing files or

incorrect formatting.

2.​ Inverting the Dictionary:


4
Programming Assignment Unit 8

​ ​ a. The invert_dictionary function loops through key-value pairs.

b. Values become keys, and keys are grouped as a list under them.

c. If a value is a string, it is converted into a list for uniform processing.

d. This process ensures multiple keys can be associated with the same value

efficiently.

3.​ Writing to a File:

a. The write_dictionary_to_file function writes the inverted dictionary to an

output file.

b. Exception handling ensures errors are caught if writing fails.

c. The program prints a success message upon completion.

This Python program successfully reads, processes, and writes dictionary data while

incorporating robust error handling mechanisms. Exception handling ensures that missing files,

incorrect formats, or unexpected issues do not cause the program to crash. By inverting the

dictionary, we restructure the data for better organization, making it easier to retrieve

information. This approach not only improves data management but also enhances efficiency in

handling key-value relationships.


5
Reference List

Downey, A. (2015). Think Python: How to Think Like a Computer Scientist (2nd ed.). Green Tea

Press.

Schafer, C. (2016, April 29). Python tutorial: File objects - Reading and writing to files [Video].

YouTube. https://youtu.be/Uh2ebFW8OYM

Python File Open. (n.d.). W3Schools. https://www.w3schools.com/python/python_file_open.asp

You might also like