python文件拆分与重组

103 篇文章 0 订阅
60 篇文章 0 订阅


文件拆分代码:

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

 

import os

import sys

import threading

 

def getFileSize(file):

    file.seek(0, os.SEEK_END)

    fileLength = file.tell()

    file.seek(00)

    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(fileFullPathdivideTotalPartsCountthreadIndexfileSize):

    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(00)

    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()

 

拆分文件演示:

源文件:

计算机生成了可选文字:N a m e . eclipse inst winE4 exe Date modified 2m7 / 占 12 1 以 47 V 0 Search test Size 4 , 6 巧 KB Application

拆分:

计算机生成了可选文字:CAWindowslSystem32Xcmd.exe - python divideFiIe.py : divideFi1e. py File path: C: exe ow many parts do you size size size size size size size size like to divide? : go 1591806 1591806 1591806 1591806 1591806 1591806 1591806Part 9 fini shed, 1591806 size 1591806 art art art art art art art art art art art art 2 fini shed, fini shed, g fini shed, 4 fini shed, 5 fini shed, fini shed, 8 fini shed, 6 fini shed, 11 fini shed, 10 fini shed, 13 fini shed, 14 fini shed, 12 fini shed, 17 fini shed, 19 fini shed, 22 fini shed, 24 fini shed, 26 fini shed, size size size size size size 1591806 1591806 1591806 1591806 1591806 1591806 size 1591806Part size 1591806 size 1591806 size 1591806 size 1591806 Part 15 fini shed, Part 16 fini shed, Part 18 fini shed, Part 20 fini shed, Part 21 fini shed, Part 23 fini shed, Part 25 fini shed, Part 28 fini shed, size size size size size size 1591806Part 1591806Part 1591806Part 1591806 1591806Part 1591806Part size 1591806Part 29 fini shed, size 1591806 size 1591806 size 1591834 art art File 27 fini shed, go fini shed, path:

拆分后文件:

计算机生成了可选文字:Name eclipse-inst-win64.exe.partI eclipse-inst-Win64.exe.part2 eclipse-inst-Win64.exe.part3 eclipse-inst-Win64.exe.part4 eclipse-inst-Win64.exe.part5 eclipse-inst-Win64.exe.part6 eclipse-inst-Win64.exe.part7 eclipse-inst-Win64.exe.part8 eclipse-inst-win64.exe.partg eclipse-inst-Win64.exe.partIO eclipse-inst-win64.exe.partII eclipse-inst-Win64.exe.part12 eclipse-inst-Win64.exe.part13 eclipse-inst-Win64.exe.part14 eclipse-inst-Win64.exe.part15 eclipse-inst-Win64.exe.part16 eclipse-inst-Win64.exe.part17 eclipse-inst-Win64.exe.part18 eclipse-inst-win64.exe.partIg eclipse-inst-Win64.exe.part20 eclipse-inst-Win64.exe.part21 eclipse-inst-Win64.exe.part22 eclipse-inst-Win64.exe.part23 eclipse-inst-Win64.exe.part24 eclipse-inst-Win64.exe.part25 eclipse-inst-Win64.exe.part26 eclipse-inst-Win64.exe.part27 eclipse-inst-Win64.exe.part28 eclipse-inst-win64.exe.part2g eclipse-inst-Win64.exe.part30 Date modified 2017/3/27 0:20 2017/3/27 0:20 2017/3/27 0:20 2017/3/27 0:20 2017/3/27 0:20 2017/3/27 0:20 2017/3/27 0:20 2017/3/27 0:20 2017/3/27 0:20 2017/3/27 0:20 2017/3/27 0:20 2017/3/27 0:20 2017/3/27 0:20 2017/3/27 0:20 2017/3/27 0:20 2017/3/27 0:20 2017/3/27 0:20 2017/3/27 0:20 2017/3/27 0:20 2017/3/27 0:20 2017/3/27 0:20 2017/3/27 0:20 2017/3/27 0:20 2017/3/27 0:20 2017/3/27 0:20 2017/3/27 0:20 2017/3/27 0:20 2017/3/27 0:20 2017/3/27 0:20 2017/3/27 0:20 Type PARTI File PART2 File PARTY File PART4 File PARTS File PARTE File PART? File PARTE File PARTg File PARTIO File PARTII File PART12 File PARTU File PART14 File PARTI 5 File PARTIE File PARTI? File PARTIE File PARTW File PART20 File PART21 File PARTU File PARTU File PARTU File PART25 File PART26 File PART27 File PARTU File PARTU File PARDO File v Search test Size 1,555 KB 1,555 KB 1,555 KB 1,555 KB 1,555 KB 1,555 KB 1,555 KB 1,555 KB 1,555 KB 1,555 KB 1,555 KB 1,555 KB 1,555 KB 1,555 KB 1,555 KB 1,555 KB 1,555 KB 1,555 KB 1,555 KB 1,555 KB 1,555 KB 1,555 KB 1,555 KB 1,555 KB 1,555 KB 1,555 KB 1,555 KB 1,555 KB 1,555 KB 1,555 KB

重组文件:

计算机生成了可选文字:C:lWindowslSystem321cmd.exe - python rebuildFiIe.py : rebuildFi1e. py File base path: C: exe ow many parts have you divided? : go File base path:

重组后文件:

计算机生成了可选文字:Name eclipse-inst-Win64.exe Date modified 2017/3/27 0:21 Type Application v Search test Size 46,635 KB

  • 14
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值