阅读仅使用特定函数写入的部分文件

2024-06-26 15:02:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个文本文件,我使用两个写函数:1)正常写入,2)安全写入。 两个write函数都将文件中开始写入的数据和偏移量作为参数。在

现在当我想从文件中读取数据时,我应该只能读取使用“正常写入”功能写入的数据,而不能读取使用“安全写入”功能写入的数据。在

当用户试图读取使用安全写入功能写入的数据时,应引发异常。此外,如果某些安全数据夹在非安全数据之间,则应仅显示非安全内容。在

例如:如果文件包含“helloHOWareyou”,其中“hello”和“areyou”是用普通写写写的,而“HOW”是用secure write写的,那么只应该读“hello”和“are you”。在

我尝试在文件头中保存安全数据的偏移量和长度,并将其与传递给read函数的偏移量进行比较。在

#Normal Write 
def writeat(self,data,offset): 
    self.file.writeat(data,offset)

#Secure Write 
def securewrite(self,data,offset):
    #Global dict to store the offset and length of secure data
    #Have to use this since it is a spec
    myvar['offset']=(offset,offset+len(data)) 
    self.file.writeat(data,offset)

#Read function
def readata(self,bytes,offset):
secureRange=mycontext['offset']
if offset in range(secureRange[0],secureRange[1]):
       raise ValueError
else:
       return self.file.readat(bytes,offset)

测试程序:

^{pr2}$

有一些成功,但帮助不大。在


Tags: 文件数据函数self功能hellodatadef