SET9
SET9
def display_menu():
print("Menu:")
print("a. Write into file")
print("b. Read all data from file")
print("c. Count and show total lines present in file")
print("d. Exit")
def write_into_file():
with open('data.txt', "a") as fo:
data = input("Enter data to write into file: ")
fo.write(data + "\n")
print("Data written to file successfully.\n")
def read_all_data():
try:
with open('data.txt', "r") as file:
content = file.read()
print("Data in file:")
print(content)
except FileNotFoundError:
print("File not found.\n")
def count_lines():
try:
with open('data.txt', "r") as fo:
lines = fo.readlines()
total_lines = len(lines)
print(f"Total lines present in file: {total_lines}\n")
except FileNotFoundError:
print("File not found.\n")
def main():
while True:
display_menu()
choice = input("Enter your choice (a-d): ")
if choice == 'a':
write_into_file()
elif choice == 'b':
read_all_data()
elif choice == 'c':
count_lines()
elif choice == 'd':
print("Exiting program.")
break
else:
print("Invalid choice. Please try again.\n")
main()
A1.
Menu:
a. Write into file
b. Read all data from file
c. Count and show total lines present in file
d. Exit
Enter your choice (a-d): a
Enter data to write into file: I have learned over the years that when one’s mind is made up, this diminishes
fear.
Data written to file successfully.
Menu:
a. Write into file
b. Read all data from file
c. Count and show total lines present in file
d. Exit
Enter your choice (a-d): b
Data in file:
I have learned over the years that when one’s mind is made up, this diminishes fear.
Menu:
a. Write into file
b. Read all data from file
c. Count and show total lines present in file
d. Exit
Enter your choice (a-d): c
Total lines present in file: 1
Menu:
a. Write into file
b. Read all data from file
c. Count and show total lines present in file
d. Exit
Enter your choice (a-d): d
Exiting program.
Q2.
create table CARDEN(
Ccode int,
CarName varchar(20),
Make varchar(20),
Colour varchar(20),
Capacity int,
Charges int
);
create table CUSTOMER(
Code int,
Cname varchar(20),
Ccode int
);