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 解析html之BeautifulSoup
Jul 07 Python
Python使用scrapy采集时伪装成HTTP/1.1的方法
Apr 08 Python
python实现图片变亮或者变暗的方法
Jun 01 Python
详解Python爬虫的基本写法
Jan 08 Python
python统计中文字符数量的两种方法
Jan 31 Python
Python变量类型知识点总结
Feb 18 Python
pandas删除指定行详解
Apr 04 Python
使用Python轻松完成垃圾分类(基于图像识别)
Jul 09 Python
Django用户认证系统 User对象解析
Aug 02 Python
python在OpenCV里实现投影变换效果
Aug 30 Python
基于python实现从尾到头打印链表
Nov 02 Python
python实现TCP文件传输
Mar 20 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中常用字符串处理代码片段整理
2011/11/07 PHP
php中ftp_chdir与ftp_cdup函数用法
2014/11/18 PHP
CL vs ForZe BO5 第三场 2.13
2021/03/10 DOTA
Jsonp 跨域的原理以及Jquery的解决方案
2010/05/18 Javascript
jQuery LigerUI 插件介绍及使用之ligerDrag和ligerResizable示例代码打包
2011/04/06 Javascript
使用基于jquery的gamequery插件做JS乒乓球游戏
2011/07/31 Javascript
IFrame跨域高度自适应实现代码
2012/08/16 Javascript
jQuery学习笔记(3)--用jquery(插件)实现多选项卡功能
2013/04/08 Javascript
函数四种调用模式以及其中的this指向
2017/01/16 Javascript
H5手机端多文件上传预览插件
2017/04/21 Javascript
微信小程序收藏功能的实现代码
2018/06/12 Javascript
jQuery实现简单的Ajax调用功能示例
2019/02/15 jQuery
js尾调用优化的实现
2019/05/23 Javascript
layui数据表格 table.render 报错的解决方法
2019/09/29 Javascript
微信小程序开发中var that =this的用法详解
2020/01/18 Javascript
vue-cli3项目配置eslint代码规范的完整步骤
2020/09/10 Javascript
[01:14]3.19DOTA2发布会 三代刀塔人第二代
2014/03/25 DOTA
Python使用CMD模块更优雅的运行脚本
2015/05/11 Python
windows系统下Python环境搭建教程
2017/03/28 Python
python和shell获取文本内容的方法
2018/06/05 Python
基于Django框架利用Ajax实现点赞功能实例代码
2018/08/19 Python
Python3对称加密算法AES、DES3实例详解
2018/12/06 Python
在python中,使用scatter绘制散点图的实例
2019/07/03 Python
如何使用python实现模拟鼠标点击
2020/01/06 Python
浅谈如何使用python抓取网页中的动态数据实现
2020/08/17 Python
html5 利用重力感应实现摇一摇换颜色可用来做抽奖等等
2014/05/07 HTML / CSS
写给妈妈的道歉信
2014/01/11 职场文书
求职面试个人自我评价
2014/02/28 职场文书
大学生学期自我鉴定
2014/03/19 职场文书
安全生产目标责任书
2014/04/14 职场文书
《画家乡》教学反思
2014/04/22 职场文书
人身损害赔偿协议书格式
2014/11/01 职场文书
2015年学校管理工作总结
2015/07/20 职场文书
优秀团员主要事迹范文
2015/11/05 职场文书
详解JavaScript中Arguments对象用途
2021/08/30 Javascript
Windows Server 2019 安装DHCP服务及相关配置
2022/04/28 Servers