当前位置: 首页 >> 榆树网-杂项 >> 文章正文

一个能“记忆”上次读取位置的cat程序

这是用python写的第一个程序,每句话都是查手册写出来的,呵呵。
大家拍砖吧。

#!/bin/env python

import os
import sys

if os.path.isfile("/tmp/workfile"):
   f = open('/tmp/workfile', 'r')
   size = int(os.fstat(f.fileno())[6])
else:
   sys.exit()

if os.path.isfile("/tmp/workfile.s"):
   s = open('/tmp/workfile.s', 'r+')
   if int(os.fstat(s.fileno())[6]) == 0 :
      last_stats = 0
   else:
      last_stats = int(s.readline())
else:
   s = open('/tmp/workfile.s', 'w')
   last_stats = 0

if size < last_stats :
   last_stats = 0

f.seek(last_stats)

notend = 1
while notend :
  string = f.readline()
  if len(string) == 0 :
     notend = 0
  else:
     string = string.strip(' \t\n\r')
     print string

s.seek(0)
s.write( str( f.tell() ) )

s.close()
f.close()
sys.exit()

随机文章

发表评论