Advertisement
Guest User

Untitled

a guest
Oct 6th, 2016
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.92 KB | None | 0 0
  1. import os
  2. import string
  3. import random
  4.  
  5.  
  6. RESULT_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'result')
  7. FILES_COUNT = 5
  8. DIRS_COUNT = 3
  9. TREE_DEPTH = 2
  10.  
  11.  
  12. def fname_generator(size=6, type='dir', path=RESULT_PATH, chars=string.ascii_uppercase + string.digits):
  13.  
  14.     def gen_name():
  15.         _name = ''.join(random.choice(chars) for _ in range(size))
  16.         return _name if type == 'dir' else '{}.{}'.format(_name, random.choice(['yoba', 'sup', 'anon', 'pruufi']))
  17.  
  18.     fpath = os.path.join(path, gen_name())
  19.     while os.path.isdir(fpath) or os.path.isfile(fpath):
  20.         fpath = os.path.join(path, gen_name())
  21.  
  22.     return fpath
  23.  
  24.  
  25. def create_files(path, remain=False):
  26.  
  27.     global FILES_COUNT
  28.  
  29.     if not remain:
  30.         try:
  31.             fcount = random.randint(1, FILES_COUNT - 1)
  32.             for i in range(fcount):
  33.                 open(os.path.join(path, fname_generator(type='file', path=path)), 'a').close()
  34.                 FILES_COUNT -= 1
  35.         except (ValueError, IndexError):
  36.             open(os.path.join(path, fname_generator(type='file', path=path)), 'a').close()
  37.             FILES_COUNT -= 1
  38.     else:
  39.         for i in range(0, FILES_COUNT):
  40.             open(os.path.join(path, fname_generator(type='file', path=path)), 'a').close()
  41.  
  42.  
  43. def rec_tree(path, depth=0):
  44.    
  45.     global DIRS_COUNT, FILES_COUNT
  46.    
  47.     if DIRS_COUNT:
  48.         if depth < TREE_DEPTH:
  49.             path = os.path.join(path, fname_generator(path=path))
  50.         else:
  51.             depth = 0
  52.             path = os.path.join(RESULT_PATH, fname_generator(path=RESULT_PATH))
  53.         os.mkdir(path)
  54.         depth += 1
  55.         DIRS_COUNT -= 1
  56.  
  57.         create_files(path)
  58.  
  59.         return rec_tree(path, depth=depth)
  60.     else:
  61.         create_files(path, remain=True)
  62.  
  63.  
  64. if __name__ == "__main__":
  65.  
  66.     if not os.path.isdir(RESULT_PATH):
  67.         os.mkdir(RESULT_PATH)
  68.        
  69.     rec_tree(RESULT_PATH)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement