19、python写文件--write

同上一篇18中介绍的读文件类似,只需要将标识符改为 ‘w’

with open('/Users/michael/test.txt', 'w') as f:
    f.write('Hello, world!')

'w'模式写入文件时,如果文件已存在,会直接覆盖(相当于删掉后新写入一个文件)。

可以传入'a'以追加(append)模式写入。

with open('/Users/michael/test.txt', 'a') as f:
    f.write('Hello, world!')
发布了70 篇原创文章 · 获赞 29 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/LOVEYSUXIN/article/details/103368938