1. 文件的写入操作
# _*_ coding:utf8 _*_
# 文件的写入
# 写入空文件
filename = 'programming.txt'
with open(filename,'w') as file_object:
file_object.write('i love yhn')
# r:读取模式 w: 写入模式 a:附加模式 r+:写入和读取模式
# 写多行文件 为了是每一行的写入都占据一行,就在后面加上回车符 '\n'
file_name = 'love.txt'
with open(file_name,'w') as file_object:
file_object.write('i love yhn\n')
file_object.write('yhn love zq\n')
# 附加到文件
# 附加模式 a
file_name = 'love.txt'
with open(file_name,'a') as file_object:
file_object.write('love\n')
file_object.write('lovelove\n')
本文介绍了Python中文件的基本操作方法,包括如何使用不同的模式打开文件进行读写,并通过实例演示了如何写入文本到文件以及如何在文件末尾追加内容。
917

被折叠的 条评论
为什么被折叠?



