python实现文件的分割与合并


Posted in Python onAugust 29, 2019

使用Python来进行文件的分割与合并是非常简单的。

python代码如下:

splitFile--将文件分割成大小为chunksize的块;

mergeFile--将众多文件块合并成原来的文件;

# coding=utf-8
import os,sys
reload(sys)
sys.setdefaultencoding('UTF-8')
 
class FileOperationBase:
 def __init__(self,srcpath, despath, chunksize = 1024):
 self.chunksize = chunksize
 self.srcpath = srcpath
 self.despath = despath
 
 def splitFile(self):
 'split the files into chunks, and save them into despath'
 if not os.path.exists(self.despath):
 os.mkdir(self.despath)
 chunknum = 0
 inputfile = open(self.srcpath, 'rb') #rb 读二进制文件
 try:
 while 1:
 chunk = inputfile.read(self.chunksize)
 if not chunk: #文件块是空的
 break
 chunknum += 1
 filename = os.path.join(self.despath, ("part--%04d" % chunknum))
 fileobj = open(filename, 'wb')
 fileobj.write(chunk)
 except IOError:
 print "read file error\n"
 raise IOError
 finally:
 inputfile.close()
 return chunknum
 
 def mergeFile(self):
 '将src路径下的所有文件块合并,并存储到des路径下。'
 if not os.path.exists(self.srcpath):
 print "srcpath doesn't exists, you need a srcpath"
 raise IOError
 files = os.listdir(self.srcpath)
 with open(self.despath, 'wb') as output:
 for eachfile in files:
 filepath = os.path.join(self.srcpath, eachfile)
 with open(filepath, 'rb') as infile:
 data = infile.read()
 output.write(data)
 
#a = "C:\Users\JustYoung\Desktop\unix报告作业.docx".decode('utf-8')
#test = FileOperationBase(a, "C:\Users\JustYoung\Desktop\SplitFile\est", 1024)
#test.splitFile()
#a = "C:\Users\JustYoung\Desktop\SplitFile\est"
#test = FileOperationBase(a, "out")
#test.mergeFile()

程序注释部分是使用类的对象的方法。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Android模拟器无法启动,报错:Cannot set up guest memory ‘android_arm’ Invalid argument的解决方法
Jul 01 Python
Python实现excel转sqlite的方法
Jul 17 Python
python opencv设置摄像头分辨率以及各个参数的方法
Apr 02 Python
对python3 一组数值的归一化处理方法详解
Jul 11 Python
详解Django的model查询操作与查询性能优化
Oct 16 Python
使用python批量化音乐文件格式转换的实例
Jan 09 Python
Python实现根据日期获取当天凌晨时间戳的方法示例
Apr 09 Python
python3.x+pyqt5实现主窗口状态栏里(嵌入)显示进度条功能
Jul 04 Python
python实现输入任意一个大写字母生成金字塔的示例
Oct 27 Python
python-视频分帧&多帧合成视频实例
Dec 10 Python
TensorFlow实现打印每一层的输出
Jan 21 Python
matplotlib基础绘图命令之errorbar的使用
Aug 13 Python
Python配置文件处理的方法教程
Aug 29 #Python
浅谈django url请求与数据库连接池的共享问题
Aug 29 #Python
python 进程的几种创建方式详解
Aug 29 #Python
python 列表推导式使用详解
Aug 29 #Python
django 数据库连接模块解析及简单长连接改造方法
Aug 29 #Python
解决Django连接db遇到的问题
Aug 29 #Python
Python pandas实现excel工作表合并功能详解
Aug 29 #Python
You might like
十天学会php之第六天
2006/10/09 PHP
PHPLog php 程序调试追踪工具
2009/09/09 PHP
php生成xml时添加CDATA标签的方法
2014/10/17 PHP
php使用event扩展的io复用测试的示例
2020/10/20 PHP
完美解决IE低版本不支持call与apply的问题
2013/12/05 Javascript
js类型转换与引用类型详解(Boolean_Number_String)
2014/03/07 Javascript
yui3的AOP(面向切面编程)和OOP(面向对象编程)
2015/05/01 Javascript
jQuery实现放大镜效果实例代码
2016/03/17 Javascript
Node.js Addons翻译(C/C++扩展)
2016/06/12 Javascript
微信小程序 wx.request(object) API详解及实例代码
2016/09/30 Javascript
如何利用模板将HTML从JavaScript中抽离
2016/10/08 Javascript
前端主流框架vue学习笔记第二篇
2017/07/26 Javascript
vue中typescript装饰器的使用方法超实用教程
2019/06/17 Javascript
Jquery属性的获取/设置及样式添加/删除操作技巧分析
2019/12/23 jQuery
JS localStorage存储对象,sessionStorage存储数组对象操作示例
2020/02/15 Javascript
js实现九宫格布局效果
2020/05/28 Javascript
[43:47]DOTA2上海特级锦标赛主赛事日 - 4 败者组第四轮#2 MVP.Phx VS Fnatic第一局
2016/03/05 DOTA
[02:49]DAC2018决赛日TOP5 LGD开启黑暗之门绝杀VP
2018/04/08 DOTA
python实现的生成随机迷宫算法核心代码分享(含游戏完整代码)
2014/07/11 Python
Python实现优先级队列结构的方法详解
2016/06/02 Python
python算法表示概念扫盲教程
2017/04/13 Python
1 行 Python 代码快速实现 FTP 服务器
2018/01/25 Python
Win8下python3.5.1安装教程
2020/07/29 Python
使用python socket分发大文件的实现方法
2019/07/08 Python
PyTorch中Tensor的维度变换实现
2019/08/18 Python
tensorflow dataset.shuffle、dataset.batch、dataset.repeat顺序区别详解
2020/06/03 Python
python 求两个向量的顺时针夹角操作
2021/03/04 Python
HTML5 Canvas旋转动画的2个代码例子(一个旋转的太极图效果)
2014/04/10 HTML / CSS
使用phonegap进行本地存储的实现方法
2017/03/31 HTML / CSS
国际贸易专业个人求职信范文分享
2013/12/14 职场文书
餐饮总经理岗位职责
2014/03/07 职场文书
校园演讲稿汇总
2014/05/21 职场文书
巴黎圣母院观后感
2015/06/10 职场文书
优秀共产党员主要事迹材料
2015/11/05 职场文书
学生早退检讨书(范文)
2019/08/19 职场文书
MySQL慢查询中的commit慢和binlog中慢事务的区别
2022/06/16 MySQL