在 Python 中写入文本文件的方式有很多种, 下面是几种常用的方式:
- 使用
open()
函数和write()
方法:
# 打开文件, 并以写入模式打开
f = open('filename.txt', 'w')
# 写入文本
f.write('hello, world!')
# 关闭文件
f.close()
在 Python 中写入文本文件的方式有很多种, 下面是几种常用的方式:
open()
函数和 write()
方法:# 打开文件, 并以写入模式打开
f = open('filename.txt', 'w')
# 写入文本
f.write('hello, world!')
# 关闭文件
f.close()