在平常的生活中,我们会遇到下面这样的情况:

你下载了一个比较大型的游戏(假设有10G),现在想跟你的同学一起玩,你需要把这个游戏拷贝给他。

然后现在有一个问题是文件太大(我们不考虑你有移动硬盘什么的情况),假设现在只有一个2G或4G的优盘,该怎么办呢?

有很多方法,例如winrar压缩的时候分成很多小卷,这里不累述。

在学习python之后,我们自己就可以解决这个问题啦。

我们可以自己写一个脚本去分割合并文件,将文件分割成适合优盘大小的小文件,在拷贝,然后再合并。

import sys,os

kilobytes =
megabytes = kilobytes*
chunksize = int(*megabytes)#default chunksize def split(fromfile,todir,chunksize=chunksize):
if not os.path.exists(todir):#check whether todir exists or not
os.mkdir(todir)
else:
for fname in os.listdir(todir):
os.remove(os.path.join(todir,fname))
partnum =
inputfile = open(fromfile,'rb')#open the fromfile
while True:
chunk = inputfile.read(chunksize)
if not chunk: #check the chunk is empty
break
partnum +=
filename = os.path.join(todir,('part%04d'%partnum))
fileobj = open(filename,'wb')#make partfile
fileobj.write(chunk) #write data into partfile
fileobj.close()
return partnum
if __name__=='__main__':
fromfile = input('File to be split?')
todir = input('Directory to store part files?')
chunksize = int(input('Chunksize to be split?'))
absfrom,absto = map(os.path.abspath,[fromfile,todir])
print('Splitting',absfrom,'to',absto,'by',chunksize)
try:
parts = split(fromfile,todir,chunksize)
except:
print('Error during split:')
print(sys.exc_info()[],sys.exc_info()[])
else:
print('split finished:',parts,'parts are in',absto)

下面是脚本运行的例子:

我们在F有一个X—MEN1.rar文件,1.26G大小,我们现在把它分割成400000000bit(大约380M)的文件。

Python 3.4. (v3.4.1:c0e311e010fc, May  , ::) [MSC v.  bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
File to be split?F:\X-MEN1.rar
Directory to store part files?F:\split
Chunksize to be split?
Splitting F:\X-MEN1.rar to F:\split by
split finished: parts are in F:\split
>>>

这是分割后的文件:

下面是文件合并脚本:

import sys,os

def joinfile(fromdir,filename,todir):
if not os.path.exists(todir):
os.mkdir(todir)
if not os.path.exists(fromdir):
print('Wrong directory')
outfile = open(os.path.join(todir,filename),'wb')
files = os.listdir(fromdir) #list all the part files in the directory
files.sort() #sort part files to read in order
for file in files:
filepath = os.path.join(fromdir,file)
infile = open(filepath,'rb')
data = infile.read()
outfile.write(data)
infile.close()
outfile.close()
if __name__=='__main__':
fromdir = input('Directory containing part files?')
filename = input('Name of file to be recreated?')
todir = input('Directory to store recreated file?') try:
joinfile(fromdir,filename,todir)
except:
print('Error joining files:')
print(sys.exc_info()[],sys.exc_info()[])

运行合并脚本,将上面分割脚本分割的文件重组:

Python 3.4. (v3.4.1:c0e311e010fc, May  , ::) [MSC v.  bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
Directory containing part files?F:\split
Name of file to be recreated?xman1.rar
Directory to store recreated file?F:\
>>>

运行之后可以看到F盘下生成了重组的xman.rar

python学习——大文件分割与合并的更多相关文章

  1. python实现大文件分割与合并

    小U盘传大电影时可以免去用winrar分割文件时的压缩和解压缩过程. file.py import sys from os.path import exists fileCount = 0 def s ...

  2. 用Python实现大文件分割

    python代码如下: import sys,os kilobytes = 1024 megabytes = kilobytes*1000 chunksize = int(200*megabytes) ...

  3. Php处理大文件-分割和合并

    分割文件 /* * 分割文件 * 默认大小 2M=10485760/5 */ function file_split($file,$block_size=10485760/5) { $block_in ...

  4. 大文件分割、命令脚本 - Python

    日志文件分割.命名 工作中经常会收到测试同学.客户同学提供的日志文件,其中不乏几百M一G的也都有,毕竟压测一晚上产生的日志量还是很可观的,xDxD,因此不可避免的需要对日志进行分割,通常定位问题需要针 ...

  5. Linux中split大文件分割和cat合并文件

    当需要将较大的数据上传到服务器,或从服务器下载较大的日志文件时,往往会因为网络或其它原因而导致传输中断而不得不重新传输.这种情况下,可以先将大文件分割成小文件后分批传输,传完后再合并文件. 1.分割 ...

  6. JAVA IO分析三:IO总结&文件分割与合并实例

    时间飞逝,马上就要到2018年了,今天我们将要学习的是IO流学习的最后一节,即总结回顾前面所学,并学习一个案例用于前面所学的实际操作,下面我们就开始本节的学习: 一.原理与概念 一.概念流:流动 .流 ...

  7. java:快速文件分割及合并

    文件分割与合并是一个常见需求,比如:上传大文件时,可以先分割成小块,传到服务器后,再进行合并.很多高大上的分布式文件系统(比如:google的GFS.taobao的TFS)里,也是按block为单位, ...

  8. (转)java:快速文件分割及合并

    文件分割与合并是一个常见需求,比如:上传大文件时,可以先分割成小块,传到服务器后,再进行合并.很多高大上的分布式文件系统(比如:google的GFS.taobao的TFS)里,也是按block为单位, ...

  9. c语言文件分割与合并

    一.综述 c语言操作文件通过文件指针FILE*,每个要操作的文件必须打开然后才能读写. 注意事项: @1分割与合并文件最好使用二进制模式即"rb"或"wb",这 ...

随机推荐

  1. [py]监控内存并出图

    监控内存出图 先将内存数据搞到数据库 已使用内存算法 used = int(total) - int(free) - int(butffers) - int(cache) pymysql模块使用 db ...

  2. [py]django url 参数/reverse和HttpResponseRedirect

    参考 需要完成以下任务 - 访问http://127.0.0.1:8000/ 返回"hello maotai"或home.html - 访问http://127.0.0.1:800 ...

  3. 申请 Let’s Encrypt 泛域名证书 及 Nginx/Apache 证书配置

    什么是 Let’s Encrypt? 部署 HTTPS 网站的时候需要证书,证书由 CA (Certificate Authority )机构签发,大部分传统 CA 机构签发证书是需要收费的,这不利于 ...

  4. [LeetCode] 161. One Edit Distance_Medium

    Given two strings s and t, determine if they are both one edit distance apart. Note: There are 3 pos ...

  5. 一些ios牛人的博客

    王巍的博客:王巍目前在日本横滨任职于LINE.工作内容主要进行Unity3D开发,8小时之外经常进行iOS/Mac开发.他的陈列柜中已有多款应用,其中番茄工作法工具非常棒.http://onevcat ...

  6. 从游戏开发到web前端——仅仅只是开始

    文章开头,请允许我随便扯扯. 一来,开头从来都是最难写的,二来,描述我现在的心情和状态以及工作背景啥的,对于大家理解后面的内容也许会有所帮助~ 2012年211大学毕业,工作4年了,一直都是做游戏前端 ...

  7. 《Enhanced LSTM for Natural Language Inference》(自然语言推理)

    解决的问题 自然语言推理,判断a是否可以推理出b.简单讲就是判断2个句子ab是否有相同的含义. 方法 我们的自然语言推理网络由以下部分组成:输入编码(Input Encoding ),局部推理模型(L ...

  8. Python 为什么sys.stdout.write 输出时后面总跟一个数字

    sys.stdout 是标准输出文件.write就是往这个文件写数据. 合起来就是打印数据到标准输出 因为-在交互模式下会输出函数返回值,而write会返回输出的字符数量.在命令行里不会显示

  9. pythonl类继承例子

    #coding=utf-8 class Person(object):    def __init__(self,name,age):        self.name=name        sel ...

  10. iphone6 inline-flex兼容问题

    在编写微信端页面时,遇到这样的问题:position属性为flex的导航栏,其li标签在其余设备上显示正常,但在iphone6上浮动错误. 究其原因,是iphone6不支持position属性中的fl ...