木的象形字,单色凌的照片,孕妇食谱三个月
文件拆分代码:
#-*-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文件拆分与重组实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Python 实现将numpy中的nan和inf,nan替换成对应的均值
python爬虫把url链接编码成gbk2312格式过程解析
网友评论