知行编程网知行编程网  2023-01-02 17:30 知行编程网 隐藏边栏  21 
文章评分 0 次,平均分 0.0
导语: 本文主要介绍了关于如何用Python实现分割合并文件的相关知识,希望可以帮到处于编程学习途中的小伙伴

如何使用 Python 拆分和合并文件

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

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

那么现在有个问题就是文件太大了(我们不考虑你有移动硬盘什么的),假设只有一个2G或者4G的U盘,怎么办?

方法有很多,比如winrar压缩的时候,分成很多小卷,这里就不一一赘述了。

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

我们可以自己写一个脚本来拆分合并文件,将文件拆分成适合U盘大小的小文件,复制,然后合并。

import sys,os
kilobytes = 1024
megabytes = kilobytes*1000
chunksize = int(200*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 = 0
    inputfile = open(fromfile,'rb')#open the fromfile
    while True:
        chunk = inputfile.read(chunksize)
        if not chunk:             #check the chunk is empty
            break
        partnum += 1
        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()[0],sys.exc_info()[1])
        else:
            print('split finished:',parts,'parts are in',absto)

下面是脚本运行的例子:

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

Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:45:13) [MSC v.1600 64 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?400000000
Splitting F:\X-MEN1.rar to F:\split by 400000000
split finished: 4 parts are in F:\split
>>>

这是分割后的文件:

如何使用 Python 拆分和合并文件

下面是文件合并脚本:

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()[0],sys.exc_info()[1])

运行合并脚本,将上面拆分脚本拆分出来的文件重新组织起来:

Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:45:13) [MSC v.1600 64 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学习网,免费的在线学习
,欢迎关注!

本文转自:https://www.jianshu.com/p/587e99c494f5

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

知行编程网
知行编程网 关注:1    粉丝:1
这个人很懒,什么都没写
扫一扫二维码分享