Saving a tag list to a file and reading it back
Write code · Challenge · §8.2, §9.6 The math helper remembers
Complete two functions that save a list of tags to a file and read it back later.
The function save_tags(tags, filename) writes the file from scratch (erasing its old content) with one tag per line. End every line with a newline.
The function load_tags(filename) reads the file and returns a list of the tags in order, with their newlines removed. Skip blank lines.
Add encoding="utf-8" to every open(). Leave the two lines at the bottom as they are.
Starter code
def save_tags(tags, filename):
# Write your code here
pass
def load_tags(filename):
# Write your code here
pass
save_tags(["python", "data", "ai"], "tags.txt")
print(load_tags("tags.txt"))Solve this problem in your browser
Python runs in your browser and checks your answer right away. Hints and the answer are on the problem page.