主要两个脚本,分别是Split.py和Join.py,字面意思上:
Split.py:就是将一个文件分拆成n个小文件,可能多数出现在大文件的时候,需要分拆处理时
Join.py:将多个小文件合并成一个大文件

  • 界面交互,命令行交互和直接命令行执行
  • 文件分拆为了不仅仅适用文本文件,才有了二进制读写的方式

1、Split.py的主要代码:

kilobytes = 1024
megabytes = kilobytes * 1000
chunksize = int(1.4 * megabytes)

def split(fromfile, todir, chunksize = chunksize):
if not os.path.exists(todir):
    os.mkdir(todir)
else:
    for fname in os.listdir(todir):
        os.remove(os.path.join(todir, fname))

partnum = 0
input = open(fromfile, 'rb')
while True:
    chunk = input.read(chunksize) #eof = empty string
    if not chunk:
        print(chunk)
        break
    
    partnum += 1
    filename = os.path.join(todir, ('part%04d' % partnum))
    fileobj = open(filename, 'wb')
    fileobj.write(chunk)
    fileobj.close()
input.close()
assert partnum <= 9999

return partnum

if __name__ == '__main__':
if len(sys.argv) == 2 and sys.argv[1] == '-help':
    print("Use: ...")
else:
    if len(sys.argv) < 3:
        interactive = True
        fromfile = input('File to be split')
        todir = input('Directory to store part files')
    else:
        interactive = False
        fromfile, todir = sys.argv[1:3]
        if len(sys.argv) == 4:
            chunksize = int(sys.argv[3])
    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)
    if interactive:
        input('Press Enter Key')

2、Join.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 ----')
else:
    if len(sys.argv) != 3:
        interactive = True
        fromdir = input('Director 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 joining files')
        print(sys.exc_info()[0], sys.exc_info()[1])
    else:
        print('join complete : see', absto)
    
    if interactive:
        input("Press Enter Key")

![afb91dbf971a80c4dc25b03b9d791985.jpg][1]
[1]: http://www.hetianxiong.com/usr/uploads/2018/08/3750224341.jpg