Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import string
- import random
- RESULT_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'result')
- FILES_COUNT = 5
- DIRS_COUNT = 3
- TREE_DEPTH = 2
- def fname_generator(size=6, type='dir', path=RESULT_PATH, chars=string.ascii_uppercase + string.digits):
- def gen_name():
- _name = ''.join(random.choice(chars) for _ in range(size))
- return _name if type == 'dir' else '{}.{}'.format(_name, random.choice(['yoba', 'sup', 'anon', 'pruufi']))
- fpath = os.path.join(path, gen_name())
- while os.path.isdir(fpath) or os.path.isfile(fpath):
- fpath = os.path.join(path, gen_name())
- return fpath
- def create_files(path, remain=False):
- global FILES_COUNT
- if not remain:
- try:
- fcount = random.randint(1, FILES_COUNT - 1)
- for i in range(fcount):
- open(os.path.join(path, fname_generator(type='file', path=path)), 'a').close()
- FILES_COUNT -= 1
- except (ValueError, IndexError):
- open(os.path.join(path, fname_generator(type='file', path=path)), 'a').close()
- FILES_COUNT -= 1
- else:
- for i in range(0, FILES_COUNT):
- open(os.path.join(path, fname_generator(type='file', path=path)), 'a').close()
- def rec_tree(path, depth=0):
- global DIRS_COUNT, FILES_COUNT
- if DIRS_COUNT:
- if depth < TREE_DEPTH:
- path = os.path.join(path, fname_generator(path=path))
- else:
- depth = 0
- path = os.path.join(RESULT_PATH, fname_generator(path=RESULT_PATH))
- os.mkdir(path)
- depth += 1
- DIRS_COUNT -= 1
- create_files(path)
- return rec_tree(path, depth=depth)
- else:
- create_files(path, remain=True)
- if __name__ == "__main__":
- if not os.path.isdir(RESULT_PATH):
- os.mkdir(RESULT_PATH)
- rec_tree(RESULT_PATH)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement