python实现表格分离计算重组_python文件拆分与重组实例

该博客介绍了如何使用Python进行文件拆分和重组。首先展示了文件拆分的代码,通过输入文件路径和拆分部分数量,将文件分割成多个部分。接着展示了文件重组的代码,读取所有部分并重新组合成原始文件。最后,给出了拆分和重组的示例过程。
摘要由CSDN通过智能技术生成

文件拆分代码:

#-*-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()

拆分文件演示:

源文件:

1265b569e646bb690f2cafd553abf1fe.png

拆分:

28caca9dcf8ac99c02bc12e9126372ba.png

拆分后文件:

67fade5739de6a21430138a42963efe7.png

重组文件:

b2ec9328317efb7e8b60f70b23b924b9.png

重组后文件:

d9922ab8eed5033dd1abd5d06078492a.png

以上这篇python文件拆分与重组实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持萬仟网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值