Skip to content

Instantly share code, notes, and snippets.

@aziascreations
Created June 25, 2021 23:41
Show Gist options
  • Save aziascreations/d97a0e8a1272f559d4e5d9a863c071a2 to your computer and use it in GitHub Desktop.
Save aziascreations/d97a0e8a1272f559d4e5d9a863c071a2 to your computer and use it in GitHub Desktop.
Split directory fixer
import os
import shutil
import json
import re
def is_contained_inside_2d_array(element, index, array):
for arr_elem in array:
print(element)
if arr_elem[index] == element:
print(element + "!!!")
return True
return False
def is_content_inside_2d_array(array1, array2, index2):
for elem2 in array2:
if elem2[index2] in array1:
return True
return False
def main(root_path):
folders = []
recurrent_sub_folder = []
for file in os.listdir(root_path):
if os.path.isdir(file):
folders.append(file)
for folder in folders:
for sub_file in os.listdir(os.path.join(root_path, folder)):
sub_file_path = os.path.join(root_path, folder, sub_file)
if os.path.isdir(sub_file_path):
is_known = False
for known_sub_folder in recurrent_sub_folder:
if known_sub_folder[0] == sub_file:
is_known = True
known_sub_folder[1] += 1
if not is_known:
new_sub_folder = [sub_file, 1]
recurrent_sub_folder.append(new_sub_folder)
for known_sub_folder in recurrent_sub_folder:
if known_sub_folder[1] == 1 or re.match("(Albums|Singles|__pycache__)", known_sub_folder[0]):
known_sub_folder.clear()
recurrent_sub_folder = [x for x in recurrent_sub_folder if x != []]
folders = [x for x in folders if (not re.match("(__pycache__)", x))
and is_content_inside_2d_array(os.listdir(os.path.join(root_path, x)), recurrent_sub_folder, 0)]
print("Found {} folders with recurrent sub-folders !".format(len(folders)))
output_root_path = os.path.join(root_path, "_output")
if os.path.isdir(output_root_path):
print("The output folder \"{}\" already exist !".format(output_root_path))
# exit(1)
else:
os.makedirs(output_root_path, exist_ok=True)
for known_folder in folders:
print("Processing {}".format(known_folder))
shutil.copytree(os.path.join(root_path, known_folder), output_root_path, dirs_exist_ok=True)
shutil.rmtree(os.path.join(root_path, known_folder))
def run():
main(os.path.dirname(os.getcwd()))
if __name__ == "__main__":
main(os.path.dirname(__file__))
os.system("pause")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment