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设计】。

相关文章

python3+PyQt5实现拖放功能

python3+PyQt5实现拖放功能

本文是对《Python Qt GUI快速编程》的第10章的例子拖放用Python3+PyQt5进行改写,对图表列表,表格等进行相互拖放,基本原理雷同,均采用setAcceptDrops(...

Python2与Python3的区别点整理

python解释器默认编码(python2与python3的区别一) python2 解释器默认编码:ascii python3 解释器默认编码:utf-8 输入(python2与p...

使用Python微信库itchat获得好友和群组已撤回的消息

使用Python微信库itchat获得好友和群组已撤回的消息

具体代码如下所述: #coding=utf-8 import itchat from itchat.content import TEXT from itchat.content i...

Python对excel文档的操作方法详解

Python对excel文档的操作方法详解

本文实例讲述了Python对excel文档的操作方法。分享给大家供大家参考,具体如下: pip安装python库:(linux命令行输入不要在idle输入) pip install...

使用python的pandas库读取csv文件保存至mysql数据库

第一:pandas.read_csv读取本地csv文件为数据框形式 data=pd.read_csv('G:\data_operation\python_book\chapter5\...