pythonlab8
pythonlab8
NO: 8
71762305009 FILES, ERRORS AND EXCEPTIONS
12.03.2025
AIM:
PROGRAM:
CODE:
except FileNotFoundError:
print("No such file exist")
OUTPUT:
OUTPUT:
OUTPUT:
CODE:
try:
a=int(input("enter number 1:"))
b=int(input("enter number 2:"))
c=a/b
except ValueError as e:
print("Input is non numeric")
except ZeroDivisionError:
print("Denominator is zero")
else:
print(f"{c}")
OUTPUT:
CODE:
except FileNotFoundError:
print("No such file exist")
OUTPUT:
CODE:
import csv
import os
def process_records():
try:
if not os.path.exists(FILE_NAME):
raise FileNotFoundError(f"{FILE_NAME} not found!")
records = []
with open(FILE_NAME, mode="r") as file:
csv_reader = csv.reader(file)
header = next(csv_reader)
name = row[0].strip()
mark = int(row[1].strip())
records.append((name, mark))
except ValueError as e:
print(f"Skipping invalid record: {row} - {e}")
if not records:
print("No valid records found.")
return
except FileNotFoundError as e:
print(f"Error: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
def main():
while True:
print("\n1. Process and Display Records from CSV")
print("2. Exit")
choice = input("Enter your choice (1/2): ")
if choice == "1":
process_records()
elif choice == "2":
print("Exiting...")
break
else:
print("Invalid choice! Please enter 1 or 2.")
if __name__ == "__main__":
main()
OUTPUT:
RESULT:
The programs on files, errors and exceptions have been implemented and
executed successfully.