此文档包括之后的Python只适用于Python3
f = open("./123.txt","w")
a=3
while(a>0):
if(a==3):
print("%s" % ("aaa"),file=f)
if(a==2):
print("%s" % ("111"),file=f)
if(a==1):
print("%s" % ("ccc"),file=f)
a=a-1
f.close()
f = open("./123.txt")
line=f.readline()
while line:
print("%s" % (line.rstrip())) #rstrip()删除末尾的换行
print (line,end='') #end='',把末尾的换行改变为空,不设置默认为换行
print("----")
line=f.readline()
f.close()
打印结果:也可以用f.write写入文件,用法与print一致
aaa
aaa
----
111
111
----
ccc
ccc
----