1.4.1 文件输入
f = open("foo.txt") #读的方式打开文件,且python3
line = f.readline()
while line:
print line, #后面更 ' , '将忽略换行符
# print (line, end = ' ')
line = f.readline()
f.close()
注意:readline()函数是读取一行内容,包括结尾的换行符在内。读至文件结尾时将返回空字符。
for line in open("foo.txt")
print (line , end = ' ')
1.4.2文件输出
f = open("out.txt", "w" )#以写的方式打开文件
while year <= numyears:
principal = principal * (1 + rate)
f.write("%3d, %0.2f" %(year, principal))
print ("%3d, %0.2f" %(year, principal), file = f)
#input("year and year ") #python3
year += 1
f.close()