# 逐行读取file="E:\学习\Python\代码.txt"withopen(file)as file_object:for line in file_object:print(line)
12
456
5
666
8
999
file="E:\学习\Python\代码.txt"withopen(file)as file_object:for line in file_object:print(line.strip())-------消除空行
12
456
5
666
8
999
withopen(file,"w")as file_object:#如果出现 not writable报错,需要加上"w"变得可写入;读取模式为"r";附加模式为"a"
file_object.write("i love you.")withopen(file)as file_object:for line in file_object:print(line)
i love you.
withopen(file,"a")as file_object:#附加模式为"a"不改变原内容
file_object.write("\ni love you,too.")withopen(file)as file_object:for line in file_object:print(line)
i love you.i love you,too.
i love you,too.
i love you,too.