Python读取、写入txt文本内容

读取

path1='D:/1.txt'
file1=open(path1,'r',encoding="utf-8")
content1=file1.read()
#content2=file1.readlines()  #读取所有行
#content3=file1.readline()  #读取所一行
print(content1)
file1.close()

写入

path2=r'D:/1.txt'
file2=open(path2,'a',encoding="utf-8")
file2.write("www.luofenming.com\n")
file2.close()

本文来自 www.luofenming.com