您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)使用python怎么實(shí)現(xiàn)文件拆分與重組,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
文件拆分代碼:
#-*-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怎么實(shí)現(xiàn)文件拆分與重組,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。