python实现大文件分割与合并


Posted in Python onJuly 22, 2019

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

下面是一种用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 命令行参数sys.argv
Sep 06 Python
如何利用Fabric自动化你的任务
Oct 20 Python
Python实现计算圆周率π的值到任意位的方法示例
May 08 Python
python实现从文件中读取数据并绘制成 x y 轴图形的方法
Oct 14 Python
python利用小波分析进行特征提取的实例
Jan 09 Python
Python饼状图的绘制实例
Jan 15 Python
Python3获取电脑IP、主机名、Mac地址的方法示例
Apr 11 Python
Python3使用TCP编写一个简易的文件下载器功能
May 08 Python
选择Python写网络爬虫的优势和理由
Jul 07 Python
Django的性能优化实现解析
Jul 30 Python
python接口自动化之ConfigParser配置文件的使用详解
Aug 03 Python
Pycharm Available Package无法显示/安装包的问题Error Loading Package List解决
Sep 18 Python
cProfile Python性能分析工具使用详解
Jul 22 #Python
python实现大文本文件分割
Jul 22 #Python
python plotly绘制直方图实例详解
Jul 22 #Python
python分割一个文本为多个文本的方法
Jul 22 #Python
在linux系统下安装python librtmp包的实现方法
Jul 22 #Python
django搭建项目配置环境和创建表过程详解
Jul 22 #Python
对python中基于tcp协议的通信(数据传输)实例讲解
Jul 22 #Python
You might like
php empty函数判断mysql表单是否为空
2010/04/12 PHP
phpMyAdmin出现无法载入 mcrypt 扩展,请检查PHP配置的解决方法
2012/03/26 PHP
php简单分页类实现方法
2015/02/26 PHP
Smarty模板简单配置与使用方法示例
2016/05/23 PHP
php+jQuery递归调用POST循环请求示例
2016/10/14 PHP
laravel开发环境homestead搭建过程详解
2020/07/03 PHP
Sample script that deletes a SQL Server database
2007/06/16 Javascript
TextArea 控件的最大长度问题(js json)
2009/12/16 Javascript
整理8个很棒的 jQuery 倒计时插件和教程
2011/12/12 Javascript
JS组件Bootstrap Select2使用方法详解
2020/04/17 Javascript
修改Jquery Dialog 位置的实现方法
2016/08/26 Javascript
利用javascript实现的三种图片放大镜效果实例(附源码)
2017/01/23 Javascript
JS奇技之利用scroll来监听resize详解
2017/06/15 Javascript
微信小程序开发中的疑问解答汇总
2017/07/03 Javascript
JS倒计时实例_天时分秒
2017/08/22 Javascript
Vue+iview+webpack ie浏览器兼容简单处理
2019/09/20 Javascript
vue中音频wavesurfer.js的使用方法
2020/02/20 Vue.js
[05:15]DOTA2英雄梦之声_第16期_灰烬之灵
2014/06/21 DOTA
python爬虫实战之爬取京东商城实例教程
2017/04/24 Python
python mysql断开重连的实现方法
2019/07/26 Python
Python基于pygame实现单机版五子棋对战
2019/12/26 Python
Python要求O(n)复杂度求无序列表中第K的大元素实例
2020/04/02 Python
2014年圣诞节倒计时网页的制作过程
2014/12/05 HTML / CSS
英国快时尚女装购物网站:PrettyLittleThing
2018/08/15 全球购物
Fox Racing英国官网:越野摩托车和山地自行车服装
2020/02/26 全球购物
如何利用XMLHTTP检测URL及探测服务器信息
2013/11/10 面试题
会计主管岗位职责
2014/01/03 职场文书
工程招投标邀请书
2014/01/30 职场文书
《狮子和兔子》教学反思
2014/03/02 职场文书
培训班主持词
2014/03/28 职场文书
水利水电专业自荐信
2014/07/08 职场文书
法人授权委托书范本
2014/09/17 职场文书
学生犯错保证书
2015/05/09 职场文书
党小组推荐意见
2015/06/02 职场文书
《鲁班学艺》读后感3篇
2019/11/27 职场文书
MySQL系列之三 基础篇
2021/07/02 MySQL