美文网首页python
python分块读文件

python分块读文件

作者: 迷糊银儿 | 来源:发表于2019-11-20 16:02 被阅读0次

一、思路

  1. 分块读取文件的所有内容,按块为单位存入到队列中(块的大小=文件的总行数/线程数)
  2. 每个线程从队列中读数据(当然单位是原来的块)

如何实现分块读???

  1. 定义一个列表A,该列表存储每一个块的起始和终止位置。
  2. 根据列表A的元素(元素为列表,存储每一个块的起始和终止位置),读取文件,并将内容存储至队列(该队列为全局变量)中。

全部代码

import threading
import time
import queue

class Reader(threading.Thread):
    def __init__(self,Thread_id):
        super(Reader, self).__init__()
        self.Thread_id=Thread_id

    def run(self):
        global q
        temp_list=q.get()
        for text in temp_list:
            colums=text.split('\n')
            print ("Thread_id",self.Thread_id,' get了:',colums)

class PartionFile:
    def __init__(self,file_name,thread_num):
        self.file_name=file_name
        self.block_num=thread_num

    def partqueue(self):
        pos_list=[]
        file_size=self.getLines()         #
        block_size=file_size/self.block_num

        global q

        start_pos=0
        for i in range(self.block_num):       #总共需记录线程数个位置对
            if i==self.block_num-1:     #如果是文件的最后一块内动,则其end_pos=file_size-1
                end_pos=file_size-1
                pos_list.append((start_pos,end_pos))
                break
            end_pos=start_pos+block_size-1
            if end_pos>=file_size:
                end_pos=file_size-1
            if start_pos>=file_size:
                break
            pos_list.append((start_pos, end_pos))
            start_pos=end_pos+1


        fd=open(self.file_name)
        for pos_item in pos_list:       #根据位置对读源文件并把文件内容存储到队列中
            temp_text=[]
            start=pos_item[0]
            end=pos_item[1]
            while start<=end:
                text=fd.readline().strip('\n')
                temp_text.append(text)
                start=start+1
            q.put(temp_text)
        fd.close

    def getLines(self):       #获取一个文件有多少行
        count=0
        fo=open(self.file_name,'r')
        for line in fo.readlines():
            count=count+1
       # print (count)
        fo.close()
        return count

if __name__=='__main__':
    file_name='sing.txt'
    q = queue.Queue()
    thread_num=4
    p=PartionFile(file_name, 4)
    p.partqueue()

    t=[]
    for i in range(thread_num):
        t.append(Reader(i))
    for i in range(thread_num):
        t[i].start()
    for i in range(thread_num):
        t[i].join()


相关文章

  • python分块读文件

    一、思路 分块读取文件的所有内容,按块为单位存入到队列中(块的大小=文件的总行数/线程数) 每个线程从队列中读数据...

  • 数据库代码 python

    python 插入数据库代码 存储工程学习 1.1python大文件分块下载,文件分块1024=1M 1.2 pa...

  • python文件分块

    File to be split?输入要分块的文件Directory to store part files?输入...

  • 源码|HDFS之DataNode:写数据块(1)

    作为分布式文件系统,HDFS擅于处理大文件的读/写。这得益于“文件元信息与文件数据分离,文件数据分块存储”的思想:...

  • 文件分块下载

    1、先检测是否支持分块下载,如果不支持,则直接下载,若支持,则将剩余内容分块下载。2、各个分块下载时保存到各自临时...

  • python基础之读文件

    python基础之读文件

  • python文件读写

    读文件: python文件对象提供了三个“读”方法: read()、readline() 和 readlines(...

  • Python学习记录之:IO编程

    IO编程 文件读写 Python中文件读写语法和C兼容 读文件使用Python内置的open()函数,传入文件名和...

  • 大文件分块上传

    大致步骤 1 图片分块 2 分块的图片 用formdata 保存在发后台 代码实现

  • Python 文件处理

    Python 读文件处理 1. readline() with 处理开闭文件&文件异常处理 readline()内...

网友评论

    本文标题:python分块读文件

    本文链接:https://www.haomeiwen.com/subject/dqioictx.html