python文件拆分与重组实例
作者:远行的风 时间:2021-10-10 18:10:25
文件拆分代码:
#-*-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()
拆分文件演示:
源文件:
拆分:
拆分后文件:
重组文件:
重组后文件:
来源:https://blog.csdn.net/qwertyupoiuytr/article/details/66616934
标签:python,拆分,重组
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
实现一个获取元素样式的函数getStyle
2009-02-10 10:37:00
ASP网站数据采集经验谈
2008-03-09 15:30:00
python numpy数组中的复制知识解析
2023-08-10 14:41:33
Python Vaex实现快速分析100G大数据量
2021-05-24 08:48:58
![](https://img.aspxhome.com/file/2023/8/80538_0s.png)
Python实现对一个函数应用多个装饰器的方法示例
2022-01-23 17:30:22
一个不错的网页拾色器
2007-09-30 19:45:00
![](https://img.aspxhome.com/file/UploadPic/20079/30/2007930201019921s.jpg)
20分钟成功编写bootstrap响应式页面 就这么简单
2023-08-12 06:12:13
![](https://img.aspxhome.com/file/2023/0/56050_0s.png)
Python获取android设备cpu和内存占用情况
2023-02-25 20:46:47
![](https://img.aspxhome.com/file/2023/3/83733_0s.png)
python使用folium库绘制地图点击框
2023-03-08 00:50:04
![](https://img.aspxhome.com/file/2023/0/84100_0s.jpg)
Flask模拟实现CSRF攻击的方法
2023-11-18 16:21:39
![](https://img.aspxhome.com/file/2023/4/78624_0s.jpg)
FrontPage服务器扩展
2008-03-05 13:05:00
已安装tensorflow-gpu,但keras无法使用GPU加速的解决
2021-08-15 12:17:42
![](https://img.aspxhome.com/file/2023/6/81736_0s.jpg)
python 中的@property的用法详解
2023-03-03 16:57:57
ThinkPHP函数详解之M方法和R方法
2023-11-22 12:00:25
python基于gevent实现并发下载器代码实例
2024-01-01 00:31:53
解决phpmyadmin中缺少mysqli扩展问题的方法
2023-07-13 02:30:12
asp如何实现网络打印?
2010-05-24 18:31:00
别开生面:纯CSS实现相册滑动浏览
2008-06-26 13:24:00
![](https://img.aspxhome.com/file/UploadPic/20086/26/2008626133524696s.jpg)
python实现域名系统(DNS)正向查询的方法
2021-03-26 17:20:57
![](https://img.aspxhome.com/file/2023/5/79805_0s.png)
JavaScript的9个陷阱及评点
2007-08-28 15:10:00