python文件拆分与重组实例

yipeiwu_com5年前Python基础

文件拆分代码:

#-*-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设计】。

相关文章

Django在admin后台集成TinyMCE富文本编辑器的例子

Django在admin后台集成TinyMCE富文本编辑器的例子

Django原生的TextField并不友好,集成TinyMCE富文本编辑器 Django版本:1.11.5 TinyMCE版本:4.6.7 第一步:从官网下载TinyMCE https...

PyQt5实现让QScrollArea支持鼠标拖动的操作方法

PyQt5实现让QScrollArea支持鼠标拖动的操作方法

如下所示: #!/usr/bin/evn python3 # -*- coding: utf-8 -*- import sys from PyQt5.QtWidgets impo...

python二分法实现实例

1.算法:(设查找的数组期间为array[low, high]) (1)确定该期间的中间位置K(2)将查找的值T与array[k]比较。若相等,查找成功返回此位置;否则确定新的查找区域,...

Python基础练习之用户登录实现代码分享

python版本为python3.5 1.要求 1)输入用户名密码 2)认证成功后显示欢迎信息 3)输错三次后锁定 2.需求分析 1)用户信息存储在文件中(login/config...

Python将多个excel文件合并为一个文件

Python将多个excel文件合并为一个文件

利用Python,将多个excel文件合并为一个文件 思路 利用python xlrd包读取excle文件,然后将文件内容存入一个列表中,再利用xlsxwriter将内容写入到一个新...