当前位置: 首页 > 图灵资讯 > 行业资讯> python如何追写内容

python如何追写内容

来源:图灵python
时间: 2024-08-08 15:52:54

1、添加到文件中,或在现有文件的末尾写入。这可以添加到参数mode中'a'完成字符。

withopen('dog_breeds.txt','a')asa_writer:
a_writer.write('\nBeagle')

2、再次检查dog__breeds.txt时,文件的开头没有改变,Beagle已经添加到文件的结尾。

>>>withopen('dog_breeds.txt','r')asreader:
>>>print(reader.read())
Pug
JackRusselTerrier
EnglishSpringerSpaniel
GermanShepherd
StaffordshireBullTerrier
CavalierKingCharlesSpaniel
GoldenRetriever
WestHighlandWhiteTerrier
Boxer
BorderTerrier
Beagle

以上是python追写内容的方法,希望对大家有所帮助。更多Python学习指导:python基础教程

本文教程操作环境:windows7系统Python 3.9.1,DELL G3电脑。