将大数据文件分割为小文件的python方法,文件分割python,做大数据处理时会遇到需要


做大数据处理时会遇到需要把大文件分割成多份小文件操作,用python 文件方法来解决很方便。但要记得测试时一定要修改要分割的文件名哦。

下面看看我自己的将大数据文件分割为小文件的python方法,供参考!

#encoding=utf-8import osdef splitfile(filepath,partialsize=1024*1024*10):    filedir,name = os.path.split(filepath)    name,ext = os.path.splitext(name)    filedir = os.path.join(filedir,name)    if not os.path.exists(filedir):        os.mkdir(filedir)            partno = 0    stream = open(filepath,'rb')    while True:        partfilename = os.path.join(filedir,name + '_' + str(partno) + ext)        print 'write start %s' % partfilename        part_stream = open(partfilename,'wb')        read_count = 0        read_size = 1024*512        read_count_once = 0        while read_count < partialsize:            read_content = stream.read(read_size)            read_count_once = len(read_content)            if read_count_once>0:                part_stream.write(read_content)            else : break                        read_count += read_count_once                part_stream.close()        if(read_count_once < read_size) : break        partno += 1    print 'done'#www.iplaypy.comif __name__ == '__main__':    splitfile(r'E:\quotelogs\quote.data',1024*1024*100)

其它python文章分割相关文章推荐:
Python文件合并与分割操作方法工具
Python读取分割压缩TXT文本文件的方法

编橙之家文章,

评论关闭