Skip to content

Instantly share code, notes, and snippets.

@homm
Last active July 14, 2016 04:32
Show Gist options
  • Save homm/c6a79ce7b445f47a74a4d296742a5af8 to your computer and use it in GitHub Desktop.
Save homm/c6a79ce7b445f47a74a4d296742a5af8 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from __future__ import print_function
import sys
import gc
from scandir import scandir
def recursive_scandir(dir):
stack = []
it = scandir(dir)
while it or stack:
it = it or stack.pop()
try:
item = next(it)
except StopIteration:
it = None
continue
yield item
if item.is_dir():
stack.append(it)
it = scandir(item.path)
def load_images(dir, close=False):
count = 0
for file in recursive_scandir(dir):
if not file.is_file():
continue
count += 1
f = open(file.path, 'rb', 0) # Mode and buffers doesn't matter
if close:
f.close()
if count % 10000 == 0:
print('>>>', count, file.path)
return count
if __name__ == '__main__':
gc.collect()
print('Tracked objects:', len(gc.get_objects()))
count = load_images(sys.argv[1], close=True)
gc.collect()
print('Total:', count)
print('Tracked objects:', len(gc.get_objects()))
count = load_images(sys.argv[1], close=False)
gc.collect()
print('Total:', count)
print('Tracked objects:', len(gc.get_objects()))
_input = getattr(__builtins__, 'raw_input', input)
_input('Press Enter to continue...')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment