将雕塑条目写入编目
既然阿拉里克已经准备好了他的编目文件,现在是时候添加一些条目了。我们将编写一个 Python 函数,以便每次他完成一件新雕塑时都能将其添加进去。
- 在
~/project中打开一个名为add_sculpture.py的脚本:
## add_sculpture.py
def add_sculpture(name, artist, date, material, catalogue_file='sculpture_catalogue.txt'):
entry = f"{name}, Created by {artist}, Date: {date}, Material: {material}\n"
with open(catalogue_file, 'a') as file:
file.write(entry)
## 示例用法:
add_sculpture('Frozen Swan', 'Alaric', '2023-01-15', 'Ice')
- 让阿拉里克通过运行脚本来添加“Frozen Swan”条目:
python3 ~/project/add_sculpture.py
要确认条目已添加,可以使用cat命令检查文件内容:
cat ~/project/sculpture_catalogue.txt
你应该会在sculpture_catalogue.txt中看到“Frozen Swan”条目:
Frozen Swan, Created by Alaric, Date: 2023-01-15, Material: Ice