zoukankan      html  css  js  c++  java
  • 文件拆分-python

    # -*- coding: utf-8 -*-
    """
    Created on Wed Jun 12 17:28:30 2019
    
    @author: **********
    """
    
    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,('data%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)
    脑子是空的不要紧,主要是不要进水······
  • 相关阅读:
    Learning KVM
    KVM HOST IN A FEW LINES OF CODE
    VM学习—实现自己的内核
    gvisor bluepillHandler + SwitchToUser
    GO语言调试利器dlv快速上手
    gvisor debug
    gvisor 系统 调用初始化
    Android开发 02
    Android开发 01
    加分项
  • 原文地址:https://www.cnblogs.com/zzuyczhang/p/14716785.html
Copyright © 2011-2022 走看看