Python2.7 分割大文本文件

#!/usr/bin/python
# Filename : SplitText.py
 
def split_txt(filename, size):
    fp = open(filename, 'rb')
    i = 0
    n = 0
    temp = open(filename+'.part'+str(i),'wb')
    buf = fp.read(1024)
    while(True):
        temp.write(buf)
        buf = fp.read(1024)
        if(buf == ''):
            print filename+'.part'+str(i)+';'
            temp.close()
            fp.close()
            return
        n += 1
        if(n == size):
            n = 0
            print filename+'.part'+str(i)+';'
            i += 1
            temp.close()
            temp = open(filename+'.part'+str(i),'wb')
 
if __name__ == '__main__':
    name = raw_input('input filename:')
    size = int(raw_input('input filesize(KB):'))
    split_txt(name, size)        #分割後每個文件size(KB)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章