1. # 写入文件
    2. def write_to_file(filename, content):
    3. with open(filename, 'w') as file:
    4. file.write(content)
    5. print("Content has been written to", filename)
    6. # 读取文件
    7. def read_from_file(filename):
    8. try:
    9. with open(filename, 'r') as file:
    10. content = file.read()
    11. print("Content read from", filename, ":", content)
    12. except FileNotFoundError:
    13. print("File not found:", filename)
    14. # 测试写入文件
    15. write_to_file("example.txt", "This is an example content.")
    16. # 测试读取文件
    17. read_from_file("example.txt")