diff --git a/1.py b/1.py new file mode 100644 index 0000000..a2091a9 --- /dev/null +++ b/1.py @@ -0,0 +1,23 @@ +import os + + +def check_or_make_folder(path): + if os.path.exists(path): + print(f'path alredy exists {path}') + else: + print(f'make folder {path}') + os.mkdir(path) + + +def make_tree_folders(soucre_di): + '''make folders from dict''' + for key, value in soucre_di.items(): + check_or_make_folder(key) + for i in value: + path = os.path.join(key, i) + check_or_make_folder(path) + + +if __name__ == '__main__': + soucre_di = {'my_project': ['settings', 'mainapp', 'adminapp', 'authapp']} + make_tree_folders(soucre_di) diff --git a/2.py b/2.py new file mode 100644 index 0000000..136dfc6 --- /dev/null +++ b/2.py @@ -0,0 +1,43 @@ +import os + + +def check_or_make_folder(path, is_file=False): + if os.path.exists(path): + print(f'path alredy exists {path}') + else: + print(f'make new folder {path}') + if is_file: + with open(os.path.join(path), 'w') as file: + file.write(' ') + else: + os.mkdir(path) + + +def make_tree_folders(source, path=False): + '''make folders from dict''' + if type(source) == str: + # print(f' in str block {source}, {type(source)}') + check_or_make_folder(os.path.join(path, source), is_file=True) + elif type(source) == list: + # print(f' in list block {source}, {type(source)}') + for i in source: + # print(i, type(i), path) + make_tree_folders(i, path) + elif type(source) == dict: + # print(f' in dict block {source}, {type(source)}') + for k, v in source.items(): + new_path = os.path.join(path, k) if path else k + check_or_make_folder(new_path) + make_tree_folders(v, new_path) + else: + print(f'not correct value {source} type {type(source)}') + + +if __name__ == '__main__': + soucre_di = { + 'my_project': [ + {'settings': ['__init__.py', 'dev.py', 'prod.py']}, + {'mainapp': [' __init__.py', 'models.py', 'views.py', {'templates': {'mainapp': ['base.html', 'index.html']}}]}, + {'authapp': ['__init__.py', 'models.py', 'views.py', {'templates': {'authapp': ['base.html', 'index.html']}}]} + ]} + make_tree_folders(soucre_di) diff --git a/3.py b/3.py new file mode 100644 index 0000000..b49853e --- /dev/null +++ b/3.py @@ -0,0 +1,16 @@ +import os +import shutil + +cwd = 'my_project/' +new_cwd = 'my_project/templates' +print(os.getcwd()) + +for root, dirs, files in os.walk(cwd): + for dir in dirs: + old_path = os.path.join(root, dir) + if old_path.endswith('templates'): + try: + shutil.copytree(old_path, new_cwd) + except FileExistsError: + new_cwd += '1' + shutil.copytree(old_path, new_cwd) diff --git a/4_5.py b/4_5.py new file mode 100644 index 0000000..e2044c1 --- /dev/null +++ b/4_5.py @@ -0,0 +1,33 @@ +import os +from os import stat +import json + + +di_sizes = {100: (0, []), 1000: (0, []), 10000: (0, []), 100000: (0, [])} +dir_path = '/run/user/1000/gvfs/smb-share:server=keenetic-2254.local,share=server/Project/GeekBrains/homework/7/some_data' + + +def get_info_file(size): + '''get file stats''' + count, tp = di_sizes[size] + count += 1 + tp.append(path.split('.')[-1]) + tp = list(set(tp)) + di_sizes[size] = (count, tp) + + +files = os.listdir(dir_path) +for file in files: + path = os.path.join(dir_path, file) + if 0 < stat(path).st_size < 100: + get_info_file(100) + elif 100 < stat(path).st_size < 1000: + get_info_file(1000) + elif 1000 < stat(path).st_size < 10000: + get_info_file(10000) + elif 10000 < stat(path).st_size < 100000: + get_info_file(100000) + +print(di_sizes) +with open('summary.json', 'w') as file: + json.dump(di_sizes, file) diff --git a/summary.json b/summary.json new file mode 100644 index 0000000..ab56c29 --- /dev/null +++ b/summary.json @@ -0,0 +1 @@ +{"100": [0, []], "1000": [10, ["bin"]], "10000": [87, ["bin"]], "100000": [902, ["bin"]]} \ No newline at end of file