你如何附加到文件? [英] How do you append to a file?

查看:26
本文介绍了你如何附加到文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何附加到文件而不是覆盖它?是否有附加到文件的特殊函数?

How do you append to the file instead of overwriting it? Is there a special function that appends to the file?

推荐答案

这可以通过将 open() 中的 mode 设置为 "a" (append) 而不是 "w"(写).请参阅文档 op open() 了解所有可用模式.

This can be achieved by setting the mode in open() to "a" (append) instead of "w" (write). See the documentation op open() for all available modes.

with open("test.txt", "a") as myfile:
    myfile.write("appended text")

这篇关于你如何附加到文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆