使用python怎么实现文件拆分与重组-创新互联
本篇文章给大家分享的是有关使用python怎么实现文件拆分与重组,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
创新互联公司自2013年起,先为申扎等服务建站,申扎等地企业,进行企业商务咨询服务。为申扎企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。文件拆分代码:
#-*-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怎么实现文件拆分与重组,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联成都网站设计公司行业资讯频道。
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
新闻名称:使用python怎么实现文件拆分与重组-创新互联
本文URL:http://lswzjz.com/article/cdepec.html