读写文本

print进行文本重定向,加上逗号可以在一行进行重定向

f = open('p.txt','w')
print >> f,"123hahahh"  ,
print >> f,"234wahahh"  
f.close()

内置方法:w--write r--read a--append

d = open('a.txt','w')
d.write('hi. \nsecond hhi..')
d.close()
d = open('a.txt','r')
help(d)
print d.readline()
print d.readline()
d.seek(0)
print d.read(100)

或者使用lincahe进行读取

import linecache
print linecache.getline("a.txt",1)
lines=linecache.getline("a.txt")
print lines
 posted on 2017-09-22 09:36  letryalpha  阅读(79)  评论(0)    收藏  举报