python文件拆分与重组实例
更新时间:2018年12月10日 09:31:25 作者:远行的风
今天小编就为大家分享一篇python文件拆分与重组实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
文件拆分代码:
#-*-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文件拆分与重组实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
20个Python random模块常用函数的应用与代码示例
随机数在计算机科学和数据科学领域中扮演着重要角色,Python的标准库中提供了random模块,用于生成各种随机数,本文将深入探讨random模块的各种函数,以及它们的应用场景和代码示例,需要的可以参考下2024-03-03Python使用functools模块中的partial函数生成偏函数
所谓偏函数即是规定了固定参数的函数,在函数式编程中我们经常可以用到,这里我们就来看一下Python使用functools模块中的partial函数生成偏函数的方法2016-07-07自动在Windows中运行Python脚本并定时触发功能实现
讲一下在Python中写好了一个脚本之后,怎么自动双击一个程序自动就跑起来。以及,怎么在Windows 10中设计定期定时触发并跑脚本,有需要的朋友可以参考下2021-09-09
最新评论