Resources | Revision Questions | Computer Science
Click on a question to view the answer
Consider a scenario where you are developing a program that reads data from a file. Describe how you would use exception handling to deal with potential errors such as the file not existing, the file being corrupted, or insufficient permissions to read the file. Include code snippets (in a language of your choice) to illustrate your approach. Explain the importance of using specific exception types rather than generic ones.
Write code to read a text file named "data.txt", count the occurrences of each word in the file (case-insensitively), and then write the word counts to a new text file named "word_counts.txt". Your code should handle potential file errors gracefully, informing the user if the input file does not exist.
You are given a CSV file named "sales_data.csv" containing sales records. Each line represents a sale and contains the following data: Product Name, Quantity, Price. Write a Python program to read this CSV file, calculate the total revenue for each product, and output the results to a new CSV file named "product_revenue.csv". The output CSV should have the columns "Product Name", "Total Revenue".