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判断变量是否已经定义的方法
Aug 18 Python
python实现基于两张图片生成圆角图标效果的方法
Mar 26 Python
Python实现简单字典树的方法
Apr 29 Python
python和ruby,我选谁?
Sep 13 Python
python Crypto模块的安装与使用方法
Dec 21 Python
tensorflow学习笔记之mnist的卷积神经网络实例
Apr 15 Python
利用Python如何生成便签图片详解
Jul 09 Python
超简单使用Python换脸实例
Mar 27 Python
Python3中函数参数传递方式实例详解
May 05 Python
Python搭建代理IP池实现获取IP的方法
Oct 27 Python
Pycharm激活码激活两种快速方式(附最新激活码和插件)
Mar 12 Python
python怎么自定义捕获错误
Jun 29 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在各种web服务器的运行模式详解
2013/06/03 PHP
php读取大文件示例分享(文件操作类)
2014/04/13 PHP
PHP的时间戳与具体时间转化的简单实现
2016/06/13 PHP
thinkPHP内置字符串截取函数用法详解
2016/11/15 PHP
php头像上传预览实例代码
2017/05/02 PHP
详细解读php的命名空间(二)
2018/02/21 PHP
JS 页面内容搜索,类似于 Ctrl+F功能的实现代码
2007/08/13 Javascript
妙用Jquery的val()方法
2012/06/27 Javascript
jquery中使用$(#form).submit()重写提交表单无效原因分析及解决
2013/03/25 Javascript
jquery获得页面元素的坐标值实现思路及代码
2013/04/15 Javascript
JS操作JSON要领详细总结
2013/08/25 Javascript
javascript实现简单的贪吃蛇游戏
2015/03/31 Javascript
jQuery带进度条全屏图片轮播特效代码分享
2020/06/28 Javascript
详解vue 中使用 AJAX获取数据的方法
2017/01/18 Javascript
BootStrap注意事项小结(五)表单
2017/03/10 Javascript
input输入框内容实时监测(附代码)
2017/08/15 Javascript
浅谈React前后端同构防止重复渲染
2018/01/05 Javascript
Spring Boot/VUE中路由传递参数的实现代码
2018/03/02 Javascript
vue 组件高级用法实例详解
2018/04/11 Javascript
详解Vue CLI3配置解析之css.extract
2018/09/14 Javascript
ES6 Proxy实现Vue的变化检测问题
2019/06/11 Javascript
使用JS location实现搜索框历史记录功能
2019/12/23 Javascript
小程序角标的添加及绑定购物车数量进行实时更新的实现代码
2020/12/07 Javascript
python完成FizzBuzzWhizz问题(拉勾网面试题)示例
2014/05/05 Python
Python使用面向对象方式创建线程实现12306售票系统
2015/12/24 Python
Python绘图Matplotlib之坐标轴及刻度总结
2019/06/28 Python
如何用Python破解wifi密码过程详解
2019/07/12 Python
pandas分批读取大数据集教程
2020/06/06 Python
pytorch 移动端部署之helloworld的使用
2020/10/30 Python
Banana Republic英国官网:香蕉共和国,GAP集团旗下偏贵族风
2018/04/24 全球购物
Theory美国官网:后现代都市风时装品牌
2018/05/09 全球购物
迪士尼英国官方商店:shopDisney UK
2019/09/21 全球购物
Engel & Bengel官网:婴儿推车、儿童房家具和婴儿设备
2019/12/28 全球购物
厉行节约工作总结
2015/08/12 职场文书
民政局2016年“六一”儿童节慰问活动总结
2016/04/06 职场文书
Redis命令处理过程源码解析
2022/02/12 Redis