您的位置:首页 > 编程语言 > Python开发

python文件读取:从任意位置读取任意长度数据

2016-12-07 17:22 274 查看
从任意位置读取任意长度数据

def funn(location,length):
path = os.path.join(os.path.abspath(os.path.dirname(__file__) + os.path.sep + ".."), "xxxx", "xxxx")
filedir = os.path.join(path,filename)
try:
with open(filedir, 'r') as fs:
fs.seek(location, 1) # location为 开始的偏移量,也就是代表需要移动偏移的字节数
databuffer = fs.read(length)
except Exception as E:
log.error(E.message)


按顺序读取文件数据

class test(object):
keeplocation = "" #全局保存读取文件的位置,循环调用funn函数,可以按顺序不断读取文件数据
def funn(self,length):
path = os.path.join(os.path.abspath(os.path.dirname(__file__) + os.path.sep + ".."), "xxxx", "xxxx")
filedir = os.path.join(path,filename)
try:
with open(filedir, 'r') as fs:
fs.seek(self.keeplocation, 1)
databuffer = fs.read(length)
self.keeplocation = fs.tell()
except Exception as E:
log.error(E.message)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: