'How to sum file size and create new folder accordingly?

I'm processing several XML files via XSLT. These files describe 4 different types of objects. What I want to do is to group them by type by creating N folders of a maximum size of 8mb. In general what I have done so far works. However, the max size of the folders that are created is 3.5mb instead of 8mb. Here the part of the code where the max size is defined

xslt = ET.parse(myXslt)
transform = ET.XSLT(xslt)
newdom = transform(dom)
for element in newdom.iter():
#Parsing D1 files
  if element.tag == 'do_system_object_id':

    do = (ET.tostring(newdom, pretty_print=True, encoding='utf-8'))
    #add new file to file_list and check wether the size of current subdirectory would now exceed 8MB
    file_do_list.append(do)                        

    if sum(len(f) for f in file_do_list) > 8000000:

      current_do_dir = myOutput+ "//D1/D1_"+ str(index_do_dir) + "//"

      index_do_dir+=1
      while os.path.exists(current_do_dir):
        current_do_dir = myOutput+ "//D1/D1_"+ str(index_do_dir) + "//"
        index_do_dir+=1
      os.makedirs(current_do_dir)
      file_do_list=[]
      file_do_list.append(do)

    outfile = open(current_do_dir + item, 'wb')
    outfile.write(do)
    #Parsing E78 files
  elif element.tag == ...

How can I fix this issue?

Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source