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 Tkinter GUI编程入门介绍
Mar 10 Python
使用Python对IP进行转换的一些操作技巧小结
Nov 09 Python
简单谈谈Python中的元祖(Tuple)和字典(Dict)
Apr 21 Python
Python自定义装饰器原理与用法实例分析
Jul 16 Python
Python爬虫常用小技巧之设置代理IP
Sep 13 Python
Python for循环与range函数的使用详解
Mar 23 Python
python读取并写入mat文件的方法
Jul 12 Python
Python super()方法原理详解
Mar 31 Python
使用tensorflow框架在Colab上跑通猫狗识别代码
Apr 26 Python
完美解决python针对hdfs上传和下载的问题
Jun 05 Python
python如何控制进程或者线程的个数
Oct 16 Python
Python基础之元组与文件知识总结
May 19 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桌面中心(一) 创建数据库
2007/03/11 PHP
PHP连接Nginx服务器并解析Nginx日志的方法
2015/08/16 PHP
php微信高级接口调用方法(自定义菜单接口、客服接口、二维码)
2016/11/28 PHP
PHP基于方差和标准差计算学生成绩的稳定性示例
2017/07/04 PHP
javascript 浏览器检测代码精简版
2010/03/04 Javascript
防止xss和sql注入:JS特殊字符过滤正则
2013/04/18 Javascript
原生js做的手风琴效果的导航菜单
2013/11/08 Javascript
js获得参数的getParameter使用示例
2014/02/26 Javascript
avalon js实现仿微博拖动图片排序
2015/08/14 Javascript
js仿3366小游戏选字游戏
2016/04/14 Javascript
基于jQuery实现仿QQ空间送礼物功能代码
2016/05/24 Javascript
玩转NODE.JS(四)-搭建简单的聊天室的代码
2016/11/11 Javascript
原生js实现图片放大缩小计时器效果
2017/01/20 Javascript
Vuex简单入门
2017/04/19 Javascript
Javascript中类式继承和原型式继承的实现方法和区别之处
2017/04/25 Javascript
JS实现验证码倒计时的注册页面
2018/01/02 Javascript
Vue中在新窗口打开页面及Vue-router的使用
2018/06/13 Javascript
小程序数据通信方法大全(推荐)
2019/04/15 Javascript
JavaScript中的执行环境和作用域链
2020/09/04 Javascript
[02:26]DOTA2英雄米拉娜基础教程
2013/11/25 DOTA
[01:30:15]DOTA2-DPC中国联赛 正赛 Ehome vs Aster BO3 第二场 2月2日
2021/03/11 DOTA
Python获取Linux系统下的本机IP地址代码分享
2014/11/07 Python
对于Python的框架中一些会话程序的管理
2015/04/20 Python
python利用标准库如何获取本地IP示例详解
2017/11/01 Python
python修改list中所有元素类型的三种方法
2018/04/09 Python
使用python装饰器计算函数运行时间的实例
2018/04/21 Python
python批量赋值操作实例
2018/10/22 Python
Python自动化之数据驱动让你的脚本简洁10倍【推荐】
2019/06/04 Python
HTML5 使用 sessionStorage 进行页面传值的方法
2018/07/02 HTML / CSS
Shopee印度尼西亚:东南亚与台湾市场最大电商平台
2018/06/17 全球购物
意大利在线高尔夫商店:Online Golf
2021/03/09 全球购物
美术毕业生求职信
2014/02/25 职场文书
房地产财务部员工岗位职责
2014/03/12 职场文书
学雷锋宣传标语
2014/06/25 职场文书
月度优秀员工获奖感言
2014/08/16 职场文书
Golang Web 框架Iris安装部署
2022/08/14 Python