-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathfile_manipulation.py
More file actions
112 lines (94 loc) · 3.34 KB
/
Copy pathfile_manipulation.py
File metadata and controls
112 lines (94 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# Define the filename for the database file
database_file = "database.txt"
# Function to display the main menu
def display_menu():
print("\n1. View Entries")
print("2. Add Entry")
print("3. Update Entry")
print("4. Delete Entry")
print("5. Exit")
# Function to view existing entries with serial numbers
def view_entries():
try:
with open(database_file, "r") as file:
entries = file.readlines()
if entries:
print("Entries:")
for i, entry in enumerate(entries, 1):
print(f"{i}. {entry.strip()}")
else:
print("No entries found.")
except FileNotFoundError:
print("Database file not found.")
# Function to add a new entry
def add_entry():
name = input("Enter name: ")
age = input("Enter age: ")
new_entry = f"Name: {name}, Age: {age}\n"
with open(database_file, "r") as file:
entries = file.readlines()
for entry in entries:
if entry.strip() == new_entry.strip():
print("Entry with the same name and age already exists.")
return # Exit the function if a duplicate entry is found
with open(database_file, "a") as file:
file.write(new_entry)
print("Entry added successfully!")
# Function to update an existing entry
def update_entry():
name_to_update = input("Enter the name to update: ")
try:
with open(database_file, "r") as file:
entries = file.readlines()
with open(database_file, "w") as file:
entry_updated = False
for entry in entries:
if entry.startswith(f"Name: {name_to_update}"):
new_name = input("Enter the new name: ")
new_age = input("Enter the new age: ")
updated_entry = f"Name: {new_name}, Age: {new_age}\n"
file.write(updated_entry)
entry_updated = True
if entry_updated:
print("Entry updated successfully!")
else:
print(f"No entry found with the name: {name_to_update}")
except FileNotFoundError:
print("Database file not found.")
# Function to delete an entry
def delete_entry():
name_to_delete = input("Enter the name to delete: ")
try:
with open(database_file, "r") as file:
entries = file.readlines()
with open(database_file, "w") as file:
deleted = False
for i, entry in enumerate(entries):
if entry.startswith(f"Name: {name_to_delete}"):
deleted = True
else:
file.write(entry)
if deleted:
print("Entry deleted successfully!")
else:
print("Entry not found.")
except FileNotFoundError:
print("Database file not found.")
# Main program
print("Welcome to the Basic File Manipulation Program!")
while True:
display_menu()
choice = input("\nSelect an option: ")
if choice == "1":
view_entries()
elif choice == "2":
add_entry()
elif choice == "3":
update_entry()
elif choice == "4":
delete_entry()
elif choice == "5":
break
else:
print("Invalid option. Please select a valid option.")
print("Exiting the program.")