Python实现分割文件及合并文件的方法


Posted in Python onJuly 10, 2015

本文实例讲述了Python实现分割文件及合并文件的方法。分享给大家供大家参考。具体如下:

分割文件split.py如下:

#!/usr/bin/python
##########################################################################
# split a file into a set of parts; join.py puts them back together;
# this is a customizable version of the standard unix split command-line 
# utility; because it is written in Python, it also works on Windows and
# can be easily modified; because it exports a function, its logic can 
# also be imported and reused in other applications;
##########################################################################
import sys, os
kilobytes = 1024
megabytes = kilobytes * 1000
chunksize = int(1.4 * megabytes)     # default: roughly a floppy
def split(fromfile, todir, chunksize=chunksize): 
 if not os.path.exists(todir):     # caller handles errors
  os.mkdir(todir)       # make dir, read/write parts
 else:
  for fname in os.listdir(todir):   # delete any existing files
   os.remove(os.path.join(todir, fname)) 
 partnum = 0
 input = open(fromfile, 'rb')     # use binary mode on Windows
 while 1:          # eof=empty string from read
  chunk = input.read(chunksize)    # get next part <= chunksize
  if not chunk: break
  partnum = partnum+1
  filename = os.path.join(todir, ('part%04d' % partnum))
  fileobj = open(filename, 'wb')
  fileobj.write(chunk)
  fileobj.close()       # or simply open().write()
 input.close()
 assert partnum <= 9999       # join sort fails if 5 digits
 return partnum
if __name__ == '__main__':
 if len(sys.argv) == 2 and sys.argv[1] == '-help':
  print 'Use: split.py [file-to-split target-dir [chunksize]]'
 else:
  if len(sys.argv) < 3:
   interactive = 1
   fromfile = raw_input('File to be split? ')  # input if clicked 
   todir = raw_input('Directory to store part files? ')
  else:
   interactive = 0
   fromfile, todir = sys.argv[1:3]     # args in cmdline
   if len(sys.argv) == 4: chunksize = int(sys.argv[3])
  absfrom, absto = map(os.path.abspath, [fromfile, todir])
  print 'Splitting', absfrom, 'to', absto, 'by', chunksize
  try:
   parts = split(fromfile, todir, chunksize)
  except:
   print 'Error during split:'
   print sys.exc_info()[0], sys.exc_info()[1]
  else:
   print 'Split finished:', parts, 'parts are in', absto
  if interactive: raw_input('Press Enter key') # pause if clicked

合并文件join_file.py如下:

#!/usr/bin/python
##########################################################################
# join all part files in a dir created by split.py, to recreate file. 
# This is roughly like a 'cat fromdir/* > tofile' command on unix, but is 
# more portable and configurable, and exports the join operation as a 
# reusable function. Relies on sort order of file names: must be same 
# length. Could extend split/join to popup Tkinter file selectors.
##########################################################################
import os, sys
readsize = 1024
def join(fromdir, tofile):
 output = open(tofile, 'wb')
 parts = os.listdir(fromdir)
 parts.sort()
 for filename in parts:
  filepath = os.path.join(fromdir, filename)
  fileobj = open(filepath, 'rb')
  while 1:
   filebytes = fileobj.read(readsize)
   if not filebytes: break
   output.write(filebytes)
  fileobj.close()
 output.close()
if __name__ == '__main__':
 if len(sys.argv) == 2 and sys.argv[1] == '-help':
  print 'Use: join.py [from-dir-name to-file-name]'
 else:
  if len(sys.argv) != 3:
   interactive = 1
   fromdir = raw_input('Directory containing part files? ')
   tofile = raw_input('Name of file to be recreated? ')
  else:
   interactive = 0
   fromdir, tofile = sys.argv[1:]
  absfrom, absto = map(os.path.abspath, [fromdir, tofile])
  print 'Joining', absfrom, 'to make', absto
  try:
   join(fromdir, tofile)
  except:
   print 'Error joining files:'
   print sys.exc_info()[0], sys.exc_info()[1]
  else:
   print 'Join complete: see', absto
  if interactive: raw_input('Press Enter key') # pause if clicked

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
Python Socket编程入门教程
Jul 11 Python
python批量修改文件名的实现代码
Sep 01 Python
Python中内置数据类型list,tuple,dict,set的区别和用法
Dec 14 Python
python实现简单socket通信的方法
Apr 19 Python
python爬虫之百度API调用方法
Jun 11 Python
Django自定义用户认证示例详解
Mar 14 Python
详解Python中的四种队列
May 21 Python
Python对象属性自动更新操作示例
Jun 15 Python
PyGame贪吃蛇的实现代码示例
Nov 21 Python
Windows 安装 Anaconda3+PyCharm的方法步骤
Jun 13 Python
python 列表、字典和集合的添加和删除操作
Dec 16 Python
pyecharts调整图例与各板块的位置间距实例
May 16 Python
Python写入数据到MP3文件中的方法
Jul 10 #Python
Python将阿拉伯数字转换为罗马数字的方法
Jul 10 #Python
Python自动登录126邮箱的方法
Jul 10 #Python
Python获取邮件地址的方法
Jul 10 #Python
python实现中文分词FMM算法实例
Jul 10 #Python
Python实现的最近最少使用算法
Jul 10 #Python
Python导入oracle数据的方法
Jul 10 #Python
You might like
有关phpmailer的详细介绍及使用方法
2013/01/28 PHP
php自定文件保存session的方法
2014/12/10 PHP
PHP 中 Orientation 属性判断上传图片是否需要旋转
2015/10/16 PHP
Yii框架页面渲染操作实例详解
2019/07/19 PHP
jscript之Read an Excel Spreadsheet
2007/06/13 Javascript
js闭包实例汇总
2014/11/09 Javascript
javascript图片预加载完整实例
2015/12/10 Javascript
让图片跳跃起来  javascript图片轮播特效
2016/02/16 Javascript
indexedDB bootstrap angularjs之 MVC DOMO (应用示例)
2016/06/20 Javascript
vuex实现简易计数器
2016/10/27 Javascript
nodejs body-parser 解析post数据实例
2017/07/26 NodeJs
webpack实现热加载自动刷新的方法
2017/07/30 Javascript
详解如何去除vue项目中的#——History模式
2017/10/13 Javascript
小程序如何使用分包加载的实现方法
2019/05/22 Javascript
vue.js实现点击图标放大离开时缩小的代码
2021/01/27 Vue.js
Python安装使用命令行交互模块pexpect的基础教程
2016/05/12 Python
使用python画个小猪佩奇的示例代码
2018/06/06 Python
Python使用cx_Freeze库生成msi格式安装文件的方法
2018/07/10 Python
解决Django生产环境无法加载静态文件问题的解决
2019/04/23 Python
Django1.11配合uni-app发起微信支付的实现
2019/10/12 Python
Python3.7基于hashlib和Crypto实现加签验签功能(实例代码)
2019/12/04 Python
python基于celery实现异步任务周期任务定时任务
2019/12/30 Python
python 双循环遍历list 变量判断代码
2020/05/04 Python
python如何随机生成高强度密码
2020/08/19 Python
pycharm 添加解释器的方法步骤
2020/08/31 Python
python快速安装OpenCV的步骤记录
2021/02/22 Python
HTML5计时器小例子
2013/10/15 HTML / CSS
潘多拉珠宝美国官方网站:Pandora US
2020/06/18 全球购物
简述DNS进行域名解析的过程
2013/12/02 面试题
兼职业务员岗位职责
2014/01/01 职场文书
代理协议书范本
2014/04/22 职场文书
拒绝黄毒毒宣传标语
2014/06/26 职场文书
2014年财务个人工作总结
2014/12/08 职场文书
招商银行收入证明
2015/06/17 职场文书
婚礼嘉宾致辞
2015/07/28 职场文书
从原生JavaScript到React深入理解
2022/07/23 Javascript