python基础5--文件操作--字符串读写
代码
# file's name : using_file.py
info = """My name is nami, I'm from HuBei province,
I like TVB television very much, but now I'm in CS"""
filename = "c:/nami.txt"
# write info to file
f = file(filename, "w")
f.write(info)
f.close()
print "Info saved successed!"
# read info from file
f = file(filename, "r")
print "The info are as follow:"
while True:
line = f.readline()
if len(line) == 0:
break
print line,
f.close()
info = """My name is nami, I'm from HuBei province,
I like TVB television very much, but now I'm in CS"""
filename = "c:/nami.txt"
# write info to file
f = file(filename, "w")
f.write(info)
f.close()
print "Info saved successed!"
# read info from file
f = file(filename, "r")
print "The info are as follow:"
while True:
line = f.readline()
if len(line) == 0:
break
print line,
f.close()
输出结果:
Info saved successed!
The info are as follow:
My name is nami, I'm from HuBei province,
I like TVB television very much, but now I'm in CS