• 技术文章 >头条

    如何用Python实现分割合并文件

    爱喝马黛茶的安东尼爱喝马黛茶的安东尼2019-12-13 17:54:51转载2080

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

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

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

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

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

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

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    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)的文件。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    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

    >>>

    这是分割后的文件:

    1a1c285415c97d6a5182568e87ffed2.png

    下面是文件合并脚本:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    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])

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

    1

    2

    3

    4

    5

    6

    7

    8

    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学习网,免费的在线学习python平台,欢迎关注!

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

    专题推荐:python 分割 合并 文件
    上一篇:Mozilla和扎克伯格夫妇资助Python40余万美元 下一篇:Python的装饰器原来是这么用的

    相关文章推荐

    • Python字符串操查找替换分割和连接方的法及使用• Python中split分割字符串

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网