Write a Python function that takes a filename and raises a custom exception if the file’s extension is not supported. The function should only allow .txt or .csv files.
Example 1:
Input: "data.json" Output: "Error: File extension not supported."
Example 2:
Input: "data.csv" Output: "Valid file extension."
Use Python’s string methods to extract the file extension.
def check_file_extension(filename): if not (filename.endswith('.txt') or filename.endswith('.csv')): raise Exception("Error: File extension not supported.") else: return "Valid file extension." print(check_file_extension("data.json")) # Output: "Error: File extension not supported." print(check_file_extension("data.csv")) # Output: "Valid file extension."
Unlock AI & Data Science treasures. Log in!