title: Most efficient way to randomly sample directory in Python
body: <p>I have a directory with millions of items in it on a fairly slow disk. I want to sample 100 of those items randomly, and I want to do it using a <code>glob</code> as well.</p>
  
  <p>One way to do it is to get a glob of every file in the directory, then sample that:</p>
  
  <pre><code>files = sorted(glob.glob('*.xml'))
  file_count = len(files)
  random_files = random.sample(
      range(0, file_count),
      100
  )
  </code></pre>
  
  <p>But this is really slow because I have to build up the big list of millions of files, which has to do a lot of disk crawling.</p>
  
  <p>Is there a faster way to do this that doesn't hit the disk as much? It doesn't have to be a perfectly distributed sample or even do exactly 100 items, provided it's fast.</p>
  
  <p>I'm thinking that:</p>
  
  <ul>
  <li>Maybe we can use the inodes to be faster?</li>
  <li>Maybe we can select items without knowing the entirety of what's on disk?</li>
  <li>Maybe there's some shortcut that can make this faster.</li>
  </ul>