1. Save and load a name list
Write code Easy ✓ CompletedWrite two functions, both using a with block:
save_names(path, names)writes each name on its own line (a later call replaces the old content).load_names(path)returns the names as a list of strings, without the newline characters.
The code then saves and loads the starter list and prints the result.
def save_names(path, names):
# write one name per line
pass
def load_names(path):
# return a list of names without the newlines
return []
save_names("team.txt", ["Asha", "Ben", "Chen"])
print(load_names("team.txt"))
def save_names(path, names):
with open(path, "w") as f:
for name in names:
f.write(name + "\n")
def load_names(path):
with open(path) as f:
return [line.strip() for line in f]
save_names("team.txt", ["Asha", "Ben", "Chen"])
print(load_names("team.txt"))