python实现大文件分割与合并

yipeiwu_com6年前Python基础

很多时候我们会面临大文件无法加载到内存,或者要传输大文件的问题。这时候就需要考虑将大文件分割为小文件进行处理了。

下面是一种用python分割与合并分件的实现。

import os
FILE_DIR = os.path.dirname(os.path.abspath(__file__))

#========================================================
# 文件操作
#========================================================
def get_filelist1(dir, postfix):
  '''
  按照后缀返回文件名列表
  INPUT -> 目录地址, 文件后缀
  OUTPUT -> 文件名列表
  '''
  return [os.path.join(dir, f) for f in os.listdir(dir) if f.endswith(postfix)]

def get_filelist2(dir, preffix):
  '''
  按照前缀返回文件名列表
  INPUT -> 目录地址, 文件前缀
  OUTPUT -> 文件名列表
  '''
  return [os.path.join(dir, f) for f in os.listdir(dir) if f.startswith(preffix)]

def get_file_postfix(filename):
  '''
  获取文件名后缀
  INPUT -> 文件名
  OUTPUT -> 文件后缀
  '''
  file = os.path.splitext(filename)
  preffix, postfix = file
  return postfix

def get_file_preffix(filename):
  '''
  获取文件名前缀
  INPUT -> 文件名
  OUTPUT -> 文件前缀
  '''
  file = os.path.splitext(filename)
  preffix, postfix = file
  return preffix

def file_chunkspilt(path, filename, chunksize):
  '''
  文件按照数据块大小分割为多个子文件
  INPUT -> 文件目录, 文件名, 每个数据块大小
  '''
  if chunksize > 0:
    filepath = path+'/'+filename
    partnum = 0
    inputfile = open(filepath, 'rb')
    while True:
      chunk = inputfile.read(chunksize)
      if not chunk:
        break
      partnum += 1
      newfilename = os.path.join(path, (filename+'_%04d' % partnum))
      sub_file = open(newfilename, 'wb')
      sub_file.write(chunk)
      sub_file.close()
    inputfile.close()
  else:
    print('chunksize must bigger than 0!')

def file_linespilt(path, filename, limit):
  '''
  文件按照行分割成多个子文件
  INPUT -> 文件目录, 文件名, 行数
  '''
  if limit > 0:
    preffix = get_file_preffix(filename)
    postfix = get_file_postfix(filename)
    file_count = 0
    l_list = []
    with open(path+'/'+filename, 'rb') as f:
      for line in f:
        l_list.append(line)
        if len(l_list) < limit:
          continue
        subfile = preffix+"_"+str(file_count)+"."+postfix
        with open(FILE_DIR+'/'+subfile, 'wb') as file:
          for l in l_list[:-1]:
            file.write(l)
          file.write(l_list[-1].strip())
          l_list=[]
          file_count += 1
  else:
    print('limit must bigger than 0!')

def file_combine(path, filename):
  '''
  子文件合并
  INPUT -> 文件目录, 文件名
  '''
  filepath = path+'/'+filename
  partnum = 0
  outputfile = open(filepath, 'wb')
  subfile_list = get_filelist2(FILE_DIR, filename+'_')
  for subfile in subfile_list:
    temp = open(subfile, 'rb')
    outputfile.write(temp.read())
    temp.close()
  outputfile.close()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python 自动安装 Rising 杀毒软件

不能使用时,或重新安装系统时,方便我们重新安装最新的版本. 但是每次安装都要点击好几次 Next 按钮,同时还要提供序列号,ID 等信息,我很讨厌这种重复工作,索性写一个小的脚本,让他自...

Python中使用gflags实例及原理解析

这篇文章主要介绍了Python中使用gflags实例及原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 安装命令pip inst...

python3.6+selenium实现操作Frame中的页面元素

python3.6+selenium实现操作Frame中的页面元素

有时网页中会嵌套一个或者多个Frame,此时我们直接去找嵌套在Frame里面的元素会抛出异常,所以在操作的时候我们需要将页面焦点切换到Frame里面,下面我们就以一个实例演示一下! 首先...

Python设置在shell脚本中自动补全功能的方法

本篇博客将会简短的介绍,如何在ubuntu中设置python自动补全功能。 需求:由于python中的内建函数较多,我们在百纳乘时,可能记不清函数的名字,同时自动补全功能,加快了我们开发...

Python基本语法之运算符功能与用法详解

本文实例讲述了Python基本语法之运算符功能与用法。分享给大家供大家参考,具体如下: 前言 在前面的博文介绍了Python的数据结构之后,接下来结合Python操作符来对Python程...