python檔案拆分與重組
檔案拆分程式碼:
#-*-encoding:utf-8-*- import os import sys import threading defgetFileSize(file): file.seek(0, os.SEEK_END) fileLength = file.tell() file.seek(0, 0) return fileLength defdivideFile(): fileFullPath = r"%s" % raw_input("File path: ").strip("\"") divideTotalPartsCount = int(raw_input 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+ filePartSender.start() for i in range(divideTotalPartsCount): sem.acquire() os.remove(fileFullPath) else: print"File doesn't exist" defseperateFilePart(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) 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(0, 0) 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() |
拆分檔案演示:
原始檔:
拆分:
拆分後文件:
重組檔案:
重組後文件: