05文件合并脚本--By Mark Lutz

'''
合并split.py创建的目录下的所有组分文件以重建文件。
依赖文件名的排序:长度必须一致。
'''

import os,sys
readsize=1024

def join(fromdir,tofile):
    output=open(tofile,'wb')
    parts=os.listdir(fromdir)
    parts.sort()
    for filename in parts:
        filepath=os.path.join(fromdir,filename)
        fileobj=open(filepath,'rb')
        while True:
            filebytes=fileobj.read(readsize)
            if not filebytes:break
            output.write(filebytes)
        fileobj.close()
    output.close()

if __name__ == '__main__':
    if len(sys.argv)==2 and sys.argv[1]=='-help':
        print('Use:join.py [from-dir-name to-file-name]')
    else:
        if len(sys.argv)!=3:
            interactive=True
            fromdir=input('Directory containing part files?')
            tofile=input('Name of file to be recreated?')
        else:
            interactive=False
            fromdir,tofile=sys.argv[1:]
        absfrom,absto=map(os.path.abspath,[fromdir,tofile])
        print('Joining',absfrom,'to make',absto)

        try:
            join(fromdir,tofile)
        except:
            print('Error joinning files')
            print(sys.exc_info()[0],sys.exc_info()[1])
        else:
            print('Join complete:see',absto)
        if interactive:input('Press Enter key')

  必要时,自己可能还要运行rar解压缩档案文件。

  此脚本使用os.listdir目录下的所有组分文件,然后对文件名列表排序以便将各个组分文件按正确的顺序拼装回去。然后逐字节复制。

  

  注意:

  1、此脚本是在二进制模式下处理文件,每次读取1kb的小块。也可以将单个组分文件一次性地读入:output.write(open(filepath,'rb').read()) ,但这样的弊端是它真正将文件整个一次性载入内存了。

  2、文件名排序:此方案依赖于组分文件目录中文件名的排序。它是对os.listdir返回的文件名调用了列表的sort方法,它隐式的要求创建分割文件时要有一致的长度和格式。为了满足这一要求,分割器在字符串格式化表达式中可采用零补齐数字的想法('part%04d'),以确保所有文件名在末尾的数字有着相同的位数(四位数)。排序时,较小数字前的字符零可确保组分文件正确排序并合并。或者,也可以把文件名中的数字抽出来,转换成int,然后通过列表sort方法的keys参数根据数值大小排序,

猜你喜欢

转载自www.cnblogs.com/start20180703/p/10332759.html