123.txt
2312
123
1233123
with open("123.txt",'r') as fp:
t=fp.read()
print(t)
with open("123.txt",'r') as fp:
for t in fp:
print(t)
with open("123.txt",'r') as fp:
t=fp.readlines()
#替换
for _ in t:
_=_.replace("2","9")
print(_.strip())
三种读取方式:
1.读取整个文件
2.遍历文件对象
3.存储为列表
mess=input("name?")
with open("name.txt",'a') as fp:
fp.write("\n")
fp.write(mess)
文件写入方式,隔行写入
写入方式为追加,如要替代,改为open(“name.txt”,‘w’)