title: Python loop over batch of files
body: <p>I want to loop over a batch of files in order to get 32 images of each sub-directory at a time (I cant load all images due to memory) e.g load img 1-32 of every dir use them and then load img 33-64 then 65-96 etc</p>
  
  <p>My directory:</p>
  
  <pre><code>Rootdir
    - dir1
      - img 1
      - img 2
      - img...
    - dir2
      - img 5000001
      - img 5000002
      - img...
    - dir3
      - img 10000001
      - img 10000002
      - img...
  </code></pre>
  
  <p>So I would need to load img1,2,..,32, 5000001,...5000032, 1000001,...10000032 at first loop then img33,34,..,64, 5000033,...5000064, 1000033,...10000064 at second loop</p>
  
  <p>Is there a way to do this properly?</p>
  
  <p>I am trying using os.walk and it allows me to loop over my directory but I don't see how I can adapt this loop to my required 32 batches?</p>
  
  <pre><code>for dirName, subdirList, fileList in os.walk(rootdir):
        print('Found directory: %s' % dirName)
        for fname in sorted(fileList):
          img_path = os.path.join(dirName, fname)
          try:
            img = load_img(img_path, target_size=None)
            imgs.append(img)
          except Exception as e:
            print(str(e), fname, i)
        #do something on imgs
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>all of your comment get me stuff like that:</p>
  
  <p>dir1/img1.jpg to dir1/img32.jpg then dir1/img33.jpg to dir1/img64.jpg then 
  ...</p>
  
  <p>then dir2/img1.jpg to dir1/img32.jpg then dir2/img33.jpg to dir2/img64.jpg then ... </p>
  
  <p>then dir3/img1.jpg to dir3/img32.jpg then dir3/img33.jpg to dir3/img64.jpg :( </p>
  
  <p>What I'm trying to achieve is:</p>
  
  <p>Files of dir1 numero 1 to 32 + files of dir2 numero 1 to 32 + files of dir3 numero 1 to 32 then</p>
  
  <p>Files of dir1 numero 33 to 64 + files of dir2 numero 33 to 64 + files of dir3 numero 33 to 64 in the same loop</p>