python文件拆分与重组实例


Posted in Python onDecember 10, 2018

文件拆分代码:

#-*-encoding:utf-8-*-

 

import os

import sys

import threading

 

def getFileSize(file):

 file.seek(0, os.SEEK_END)

 fileLength = file.tell()

 file.seek(0, 0)

 return fileLength

 

def divideFile():

 fileFullPath = r"%s" % raw_input("File path: ").strip("\"")

 divideTotalPartsCount = int(raw_input("How many parts do you like to divide?: "))

 if os.path.exists(fileFullPath):

  file = open(fileFullPath, 'rb')

  fileSize = getFileSize(file)

  file.close()

  # send file content

  for i in range(divideTotalPartsCount):

   filePartSender = threading.Thread(target=seperateFilePart, args=(fileFullPath, divideTotalPartsCount, i+1, fileSize))

   filePartSender.start()

  

  for i in range(divideTotalPartsCount):

   sem.acquire()

  os.remove(fileFullPath)

 else:

  print "File doesn't exist"

 

def seperateFilePart(fileFullPath, divideTotalPartsCount, threadIndex, fileSize):

 try:

  # calculate start position and end position

  filePartSize = fileSize / divideTotalPartsCount

  startPosition = filePartSize * (threadIndex - 1)

  #print "Thread : %d, startPosition: %d" % (threadIndex, startPosition)

  endPosition = filePartSize * threadIndex - 1

  if threadIndex == divideTotalPartsCount:

   endPosition = fileSize - 1

   filePartSize = fileSize - startPosition

  file = open(fileFullPath, "rb")

  file.seek(startPosition)

  filePartName = fileFullPath + ".part" + str(threadIndex)

  filePart = open(filePartName, "wb")

  lengthWritten = 0

  while lengthWritten < filePartSize:

   bufLen = 1024

   lengthLeft = filePartSize - lengthWritten

   if lengthLeft < 1024:

    bufLen = lengthLeft

   buf = file.read(bufLen)

   filePart.write(buf)

   lengthWritten += len(buf)

  filePart.close()

  file.close()

  sem.release()

  print "Part %d finished, size %d" % (threadIndex, filePartSize)

 except Exception, e:

  print e

 

sem = threading.Semaphore(0)

while True:

 divideFile()

文件重组代码:

#-*-encoding:utf-8-*-

import os

def getFileSize(file):

 file.seek(0, os.SEEK_END)

 fileLength = file.tell()

 file.seek(0, 0)

 return fileLength

 

def rebuildFile():

 fileFullPath = r"%s" % raw_input("File base path: ").strip("\"")

 divideTotalPartsCount = int(raw_input("How many parts have you divided?: "))

 file = open(fileFullPath, "wb")

 for i in range(divideTotalPartsCount):

  filePartName = fileFullPath + ".part" + str(i+1)

  filePart = open(filePartName, "rb")

  filePartSize = getFileSize(filePart)

  lengthWritten = 0

  while lengthWritten < filePartSize:

   bufLen = 1024

   buf = filePart.read(bufLen)

   file.write(buf)

   lengthWritten += len(buf)

  filePart.close()

  os.remove(filePartName)

 file.close()

 

while True:

 rebuildFile()

拆分文件演示:

源文件:

python文件拆分与重组实例

拆分:

python文件拆分与重组实例

拆分后文件:

python文件拆分与重组实例

重组文件:

python文件拆分与重组实例

重组后文件:

python文件拆分与重组实例

以上这篇python文件拆分与重组实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python代理抓取并验证使用多线程实现
May 03 Python
python实现异步回调机制代码分享
Jan 10 Python
在Python中使用异步Socket编程性能测试
Jun 25 Python
Python获取Linux系统下的本机IP地址代码分享
Nov 07 Python
使用Python中的greenlet包实现并发编程的入门教程
Apr 16 Python
使用Py2Exe for Python3创建自己的exe程序示例
Oct 31 Python
python接口自动化(十六)--参数关联接口后传(详解)
Apr 16 Python
最新2019Pycharm安装教程 亲测
Feb 28 Python
Python类中的装饰器在当前类中的声明与调用详解
Apr 15 Python
Python unittest单元测试框架实现参数化
Apr 29 Python
opencv+python实现鼠标点击图像,输出该点的RGB和HSV值
Jun 02 Python
微信小程序调用python模型
Apr 21 Python
Python对excel文档的操作方法详解
Dec 10 #Python
使用python进行拆分大文件的方法
Dec 10 #Python
python使用udp实现聊天器功能
Dec 10 #Python
Python面向对象之类和对象实例详解
Dec 10 #Python
详解Django-auth-ldap 配置方法
Dec 10 #Python
Python网页正文转换语音文件的操作方法
Dec 09 #Python
python 使用 requests 模块发送http请求 的方法
Dec 09 #Python
You might like
php调用MySQL存储过程的方法集合(推荐)
2013/07/03 PHP
用js做一个小游戏平台 (一)
2009/12/29 Javascript
jQuery获取浏览器中的分辨率实现代码
2013/04/23 Javascript
js图片翻书效果代码分享
2015/08/20 Javascript
详解js中==与===的区别
2017/01/08 Javascript
Reactjs实现通用分页组件的实例代码
2017/01/19 Javascript
通过jsonp获取json数据实现AJAX跨域请求
2017/01/22 Javascript
bootstrap table操作技巧分享
2017/02/15 Javascript
详解Vue.js之视图和数据的双向绑定(v-model)
2017/06/23 Javascript
Vue组件选项props实例详解
2017/08/18 Javascript
Nodejs Express 通过log4js写日志到Logstash(ELK)
2018/08/30 NodeJs
java实现单链表增删改查的实例代码详解
2019/08/30 Javascript
小程序两种滚动公告栏的实现方法
2019/09/17 Javascript
Jquery ajax书写方法代码实例解析
2020/06/12 jQuery
vue 使用class创建和清除水印的示例代码
2020/12/25 Vue.js
Python3利用SMTP协议发送E-mail电子邮件的方法
2017/09/30 Python
Python实现的rsa加密算法详解
2018/01/24 Python
小白入门篇使用Python搭建点击率预估模型
2018/10/12 Python
对Python3 序列解包详解
2019/02/16 Python
python 字符串常用函数详解
2019/09/11 Python
Python数据可视化:泊松分布详解
2019/12/07 Python
Python爬虫设置ip代理过程解析
2020/07/20 Python
Python生成器generator原理及用法解析
2020/07/20 Python
Python实现LR1文法的完整实例代码
2020/10/25 Python
Python3+PyCharm+Django+Django REST framework配置与简单开发教程
2021/02/16 Python
如何在Canvas上的图形/图像绑定事件监听的实现
2020/09/16 HTML / CSS
Vilebrequin美国官方网上商店:法国豪华泳装品牌
2020/02/22 全球购物
Fanatics官网:运动服装、球衣、运动装备
2020/10/12 全球购物
C#和SQL Server的面试题
2016/08/12 面试题
2014婚礼司仪主持词
2014/03/14 职场文书
《黄山奇石》教学反思
2014/04/19 职场文书
小学班长竞选演讲稿
2014/04/24 职场文书
房地产经营管理专业自荐信
2014/09/02 职场文书
2014年社区工会工作总结
2014/12/18 职场文书
2015年大学教师工作总结
2015/05/20 职场文书
《金色的草地》教学反思
2016/02/17 职场文书