python文件拆分与重组实例
程序员文章站
2022-10-07 18:55:42
文件拆分代码:
#-*-encoding:utf-8-*-
import os
import sys
import threading...
文件拆分代码:
#-*-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个快速是目标关键词参与排名的技巧