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 相关文章推荐
Python urllib、urllib2、httplib抓取网页代码实例
May 09 Python
PyCharm使用教程之搭建Python开发环境
Jun 07 Python
浅析Python中的getattr(),setattr(),delattr(),hasattr()
Jun 14 Python
python实现汽车管理系统
Nov 30 Python
python中partial()基础用法说明
Dec 30 Python
Python eval的常见错误封装及利用原理详解
Mar 26 Python
Python FtpLib模块应用操作详解
Dec 12 Python
使用python的turtle函数绘制一个滑稽表情
Feb 28 Python
django-csrf使用和禁用方式
Mar 13 Python
如何将PySpark导入Python的放实现(2种)
Apr 26 Python
5行Python代码实现图像分割的步骤详解
May 25 Python
python文件操作seek()偏移量,读取指正到指定位置操作
Jul 05 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 __autoload()方法真的影响性能吗?
2012/03/30 PHP
Windows下部署Apache+PHP+MySQL运行环境实战
2012/08/31 PHP
基于php iconv函数的使用详解
2013/06/09 PHP
php获取开始与结束日期之间所有日期的方法
2016/11/29 PHP
javascript 验证日期的函数
2010/03/18 Javascript
JavaScript中的私有/静态属性介绍
2012/07/26 Javascript
JS文本框不能输入空格验证方法
2013/03/19 Javascript
利用javascript实现禁用网页上所有文本框,下拉菜单,多行文本域
2013/12/14 Javascript
探寻Javascript执行效率问题
2014/11/12 Javascript
AngularJS基础教程之简单介绍
2015/09/27 Javascript
jquery的ajax提交form表单的两种方法小结(推荐)
2016/05/25 Javascript
javaScript知识点总结(必看篇)
2016/06/10 Javascript
jQuery Validate验证表单时多个name相同的元素只验证第一个的解决方法
2016/12/24 Javascript
js+canvas实现滑动拼图验证码功能
2018/03/26 Javascript
vue-router 源码实现前端路由的两种方式
2018/07/02 Javascript
Jquery滑动门/tab切换实现方法完整示例
2020/06/05 jQuery
vue路由权限校验功能的实现代码
2020/06/07 Javascript
原生js实现自定义难度的扫雷游戏
2021/01/22 Javascript
Python Socket传输文件示例
2017/01/16 Python
Python随机生成手机号、数字的方法详解
2017/07/21 Python
在python中bool函数的取值方法
2018/11/01 Python
win7下 python3.6 安装opencv 和 opencv-contrib-python解决 cv2.xfeatures2d.SIFT_create() 的问题
2019/10/24 Python
python numpy生成等差数列、等比数列的实例
2020/02/25 Python
python实现逆滤波与维纳滤波示例
2020/02/26 Python
python代码区分大小写吗
2020/06/17 Python
Mountain Warehouse波兰官方网站:英国户外品牌
2019/08/29 全球购物
一加手机美国官方网站:OnePlus美国
2019/09/19 全球购物
链表面试题-一个链表的结点结构
2015/05/04 面试题
什么是"引用"?申明和使用"引用"要注意哪些问题?
2016/03/03 面试题
介绍一下Mysql的存储引擎
2015/02/12 面试题
优秀毕业生求职信范文
2014/01/02 职场文书
生日礼品店创业计划书范文
2014/03/21 职场文书
2015年电教工作总结
2015/05/26 职场文书
victoriaMetrics库布隆过滤器初始化及使用详解
2022/04/05 Golang
Redis中key的过期删除策略和内存淘汰机制
2022/04/12 Redis
mysql数据库实现设置字段长度
2022/06/10 MySQL