Programming-Challenges/28 File handling/exercise-01.py
2026-08-31 12:48:25 +01:00

37 lines
580 B
Python

students = [
"john",
"jane",
"bob",
"alice",
"charlie",
"david",
"emily",
"frank",
"grace",
"harry",
"isabella",
"jessica",
]
with open("students.txt", "w") as f:
for student in students:
f.write(f"{student}\n")
students = []
with open("students.txt", "r") as f:
for line in f:
students.append(line.strip())
print("Unsorted: ")
print(students)
students.sort()
print("")
print("Sorted: ")
print(students)
with open("students.txt", "w") as f:
for student in students:
f.write(f"{student}\n")