Dec-26-2018, 03:20 AM
Having some fun with recursion.
This one places a file in the specified directory as well as its subdirectories and their subdirectories etc. In other words don't specify the file path to be '/'. hahahaha.... unless you write the code to undo it
This one places a file in the specified directory as well as its subdirectories and their subdirectories etc. In other words don't specify the file path to be '/'. hahahaha.... unless you write the code to undo it
#! /usr/bin/python3 from os.path import isdir, basename from os import listdir, system, getcwd, chdir import sys # recursively create a file in all subdirectories of a given # directory.. all dirs/subdirs should have different names def get_diff(available, removal): difference = [] s1 = set() s2 = set() s1.update(available) s2.update(removal) for i in s1.difference(s2): difference.append(i) return difference def create(finished_folders): chdir("/home/absolute/path/to/directory") current_dir = basename(getcwd()) available_dirs = listdir(".") removal_list = [] for d in available_dirs: if d in finished_folders or not isdir(d): removal_list.append(d) available_dirs = get_diff(available_dirs, removal_list) if current_dir not in finished_folders: while len(available_dirs) > 0: next_dir = available_dirs.pop() if next_dir not in finished_folders and isdir(next_dir): chdir(next_dir) available_dirs = listdir(".") for directory in available_dirs: if directory in finished_folders or not isdir(directory): removal_list.append(directory) available_dirs = get_diff(available_dirs, removal_list) system("touch test.txt") finished_folders += [basename(getcwd())] create(finished_folders) else: print("FINISHED") sys.exit() if __name__ == "__main__": folders = [] create(folders)