-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20.py
More file actions
49 lines (34 loc) · 1004 Bytes
/
20.py
File metadata and controls
49 lines (34 loc) · 1004 Bytes
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
#example 1
friends = []
for i in range(3):
name = input(f"Enter friend {i+1} name: ")
friends.append(name)
with open("friends.txt", "w") as file:
for friend in friends:
file.write(friend + "\n")
print("Friends saved to friends.txt")
#example 2
name = input("Enter student name: ")
marks = input("Enter marks: ")
with open("marks.txt", "a") as file:
file.write(f"{name} - {marks}\n")
print("Data appended to marks.txt")
#example 3
filename = input("Enter file name: ")
with open(filename, "r") as file:
lines = file.readlines()
count = len(lines)
print(f"Total number of lines: {count}")
#example 4
# Ask user for name to search
search_name = input("Enter name to search: ")
found = False
with open("friends.txt", "r") as file:
for line in file:
if search_name.lower() == line.strip().lower():
found = True
break
if found:
print("Found!")
else:
print("Not Found!")