The document outlines a series of programming tasks focused on file handling and error management in Python. It includes exercises for validating user input, handling missing files, writing employee records, performing safe divisions, and reading from files with error handling. Each task emphasizes the use of try and except blocks to manage exceptions effectively.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
8 views2 pages
Study Unit 6 Handling Files
The document outlines a series of programming tasks focused on file handling and error management in Python. It includes exercises for validating user input, handling missing files, writing employee records, performing safe divisions, and reading from files with error handling. Each task emphasizes the use of try and except blocks to manage exceptions effectively.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2
Study Unit 6 – Files
V2 - Files and Exceptions
1. Validating Number Input Write a program that asks the user to enter a number. Use a try and except to catch any error if the user does not enter a number. If the input is valid, print the number. If not, print an error message.
2. Handling Missing Files
Write a program that tries to open a file named data.txt and read its contents. If the file does not exist, catch the error and print an error message.
3. Writing Employee Records Safely
Write a program that creates a file named Employees.txt and writes the following names in the file:
• John Doe • Jane Dunning • James Potter
Add error handling in case the file cannot be created or written to.
4. Safe Division with Error Handling
Write a program that asks the user to enter two numbers. Divide the first number by the second. Use try and except to handle invalid input and also division by zero.
5. Saving Trip Names to a File
Write a program that asks the user to enter three names (one at a time). Save each name on a new line in a file called Trip.txt. Use try and except to handle any errors that may happen during writing.
6. Reading and Summing Sales Values
Create a program that opens a file named sales.txt, reads three lines from it, and tries to add them as numbers. If any line is not a number, catch the error and skip it. Show the total at the end. 7. Reading a File with Confirmation Write a program that asks the user to enter a file name. Try to open and read the file. If the file is found, print its contents and display a success message using an else clause. If the file is not found, print an error message.
8. Trip Contributions with Validation
Write a program that asks the user for three names and the amount each person wants to contribute to a trip. Save each name and amount to a file called Trip.txt. Use error handling in case the user enters something that is not a number.
9. Reading with a Final Message
Write a program that tries to open and read from a file called Employees.txt. If the file is not found, print an error message. Use a finally clause to print a message that always runs at the end.