Files and Operations
1a.PROGRAM
f=open("hello.txt","w")
f.write("Hello there!!!\n")
f.close()
print("Written to the file successfully")
OUTPUT
Written to the file successfully
1b.PROGRAM
f=open("hello.txt","r")
x=f.read()
print(x)
f.close()
OUTPUT
Hello there!!!
1c.PROGRAM
f=open("hello.txt","a")
f.write("How are you?")
f.close()
print("Appended to the file successfully")
OUTPUT
Appended to the file successfully
2a .PROGRAM
def divide_numbers():
try:
numerator = float(input("Enter the numerator:
"))
denominator = float(input("Enter the
denominator: "))
result = numerator / denominator
print(f"Result: {result}")
except ZeroDivisionError:
print("Error: Cannot divide by zero. Please
provide a non-zero denominator.")
except ValueError:
print("Error: Please enter valid numeric
inputs.")
divide_numbers()
OUTPUT
Enter the numerator: 10
Enter the denominator: 2
Result: 5. 0
2b.PROGRAM
def read_file():
file_name = input("Enter the name of the file to
read: ")
try:
with open(file_name, 'r') as file:
content = file.read()
print(content)
except FileNotFoundError:
print(f"Error: The file '{file_name}' was not
found. Please check the file path.")
except PermissionError:
print(f"Error: You don't have permission to
read the file '{file_name}'.")
read_file()
OUTPUT
Enter the numerator: 10
Enter the denominator: 0
Error: Cannot divide by zero. Please provide a non-zero
denominator.
2c.PROGRAM
def process_input():
try:
num1 = int(input("Enter the first number:
"))
num2 = int(input("Enter the second
number: "))
result = num1 / num2
print(f"Result: {result}")
except ZeroDivisionError:
print("Error: Cannot divide by zero.")
except ValueError:
print("Error: Please enter valid integers.")
except Exception as e:
print(f"An unexpected error occurred: {e}")
process_input()
OUTPUT
Enter the numerator: 10
Enter the denominator: abc
Error: Please enter valid numeric inputs.
3a .PROGRAM
import pickle
def serialize_dict_to_file():
my_dict = {'name': 'John', 'age': 30, 'city': 'New
York'}
with open('my_dict.pkl', 'wb') as file:
pickle.dump(my_dict, file)
print("Dictionary has been serialized and
saved to 'my_dict.pkl'.")
serialize_dict_to_file()
OUTPUT
Dictionary has been serialized and saved to 'my_dict.pkl'.
3b.PROGRAM
import pickle
def load_pickled_object():
try:
with open('my_dict.pkl', 'rb') as file:
loaded_dict = pickle.load(file)
print("Pickled object loaded
successfully:")
print(loaded_dict)
except FileNotFoundError:
print("Error: The file 'my_dict.pkl' does not
exist.")
except pickle.UnpicklingError:
print("Error: There was an issue unpickling
the file.")
load_pickled_object()
OUTPUT
Pickled object loaded successfully:
{'name': 'John', 'age': 30, 'city': 'New York'}
3c.PROGRAM
import pickle
def pickle_and_unpickle_list():
my_list = [1, 2, 3, 4, 5]
with open('my_list.pkl', 'wb') as file:
pickle.dump(my_list, file)
print("List has been pickled and saved to
'my_list.pkl'.")
try:
with open('my_list.pkl', 'rb') as file:
loaded_list = pickle.load(file)
print("Pickled list loaded successfully:")
print(loaded_list)
except FileNotFoundError:
print("Error: The file 'my_list.pkl' does not
exist.")
except pickle.UnpicklingError:
print("Error: There was an issue unpickling
the file.")
pickle_and_unpickle_list()
OUTPUT
List has been pickled and saved to 'my_list.pkl'.
Pickled list loaded successfully:
[1, 2, 3, 4, 5]