import os def chunk_file(filepath=None, split_len=20000): files = [] # line count of each chunked file orig = open(filepath, 'r') path, ext = os.path.splitext(filepath) file_chunk = open('%s_%s%s' % (path, file_no, ext), 'w') file_no = 0 file_line = 0 for line in orig: if file_line > split_len: # this file has finished file_chunk.close() file_no += 1 file_line = 0 # start the file path = '%s_%s%s' % (path, file_no, ext) file_chunk = open(path, 'w') files.append(path) print 'Chunked at', file_line * (file_no + 1), path file_chunk.write(line) file_line += 1 file_chunk.close()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question