Python 文件操作

1.数据写入一行,读取一行。

from datetime import datetime
import time

while True:
    time.sleep(2)
    
    f = open('d:\\abc.txt', 'a+')
  #在写入前读取位置 pos
= f.tell() f.write("this time:" + datetime.now().strftime('%H:%M:%S') + '\n')
  #在写入后读取位置 new_pos
= f.tell()
#移动写入之前的位置 f.seek(pos,0) line
= f.read() print(line)
#移动到最后的位置 f.seek(new_pos)
print('pos: %d , new_pos: %d\n' %(pos, new_pos))

 

posted @ 2017-04-12 18:50  白灰  阅读(175)  评论(0)    收藏  举报