20.2 File Processing and Exception Handling (3)

Resources | Revision Questions | Computer Science

Login to see all questions

Click on a question to view the answer

1.

Question 3

Write a Python program that takes a directory path as input. The program should recursively traverse the directory, identify all files with the extension ".txt", and create a new text file named "all_text_files.txt". Each line in the new file should contain the full path to one of the identified text files.

2.

Question 1

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.

3.

Question 2

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".