Python檔案操作:從文字末尾逆序讀取資料
阿新 • • 發佈:2019-02-15
class TraceManagerBase(object): dblocation = "" def __init__(self): self.init() def init(self): self.dblocation = os.path.join(os.path.join(os.path.abspath(os.path.dirname(__file__) + os.path.sep + ".."), "data","traceCoords")) # os.getcwd(),"data","traceCoords" if not os.path.exists(self.dblocation): os.makedirs(self.dblocation) #從檔案末尾向前讀取,逆序 def get1(self,sn,n): filename = "%s.txt" % sn path = os.path.join(self.dblocation, filename) data = [] with open(path,'r') as fs: for i in range(n+1): if i>0: fs.seek(-113*i,2) #113為每行文字的長度,包含\r\n data.append(fs.readline()) print fs.tell() #讀取後幾條 # fs.seek(-113*n,2) # print fs.tell() # data.append(fs.readlines()) # print fs.tell() print data