def split_xml(filename):
    """ The function gets the filename of wiktionary.xml.bz2 file as  input and creates
    smallers chunks of it in a the diretory chunks
    """
if not os.path.exists('chunks'):
    os.mkdir('chunks')
pagecount = 0
filecount = 1
chunkname = lambda filecount: os.path.join('chunks', 'chunk-' + str(
    filecount) + '.xml.bz2')
chunkfile = bz2.BZ2File(chunkname(filecount), 'w')
bzfile = bz2.BZ2File(filename)
for line in bzfile:
    chunkfile.write(line)
if '</page>' in line:
    pagecount += 1
if pagecount > 1999:
    chunkfile.close()
pagecount = 0
filecount += 1
chunkfile = bz2.BZ2File(chunkname(filecount), 'w')
try:
    chunkfile.close()
except:
    print('Files already close')
if __name__ == '__main__':
    split_xml('wiki-files/tawiktionary-20110518-pages-articles.xml.bz2')
How to split large wikipedia dump .xml.bz2 files in Python?