1. 程式人生 > >python檔案拆分與重組

python檔案拆分與重組



檔案拆分程式碼:

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

import os

import sys

import threading

defgetFileSize(file):

file.seek(0, os.SEEK_END)

    fileLength = file.tell()

file.seek(00)

return fileLength

defdivideFile():

    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"

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

exceptException, e:

print e

sem = threading.Semaphore(0)

whileTrue:

    divideFile()

檔案重組程式碼:

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

import os

defgetFileSize(file):

file.seek(0, os.SEEK_END)

    fileLength = file.tell()

file.seek(00)

return fileLength

defrebuildFile():

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

whileTrue:

    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