Hi everyone python newbie here.
I am trying to write a little code, my main goal is move subfolders into the another subfolder which all of them under the same folder. The files have somewhat consistent specific naming system and I am using that.
English is not my main language so I am adding the screenshots to explain myself better.
def selective copy(src): for root, subfolders, filenames in os.walk(src): for foldername in subfolders: if "Other" in foldername: other_folder = os.path.join(root, foldername) for root2, subfolders2, filenames2 in os.walk(src): for foldername2 in subfolders2: if "FA" in foldername2: fa_file = os.path.join(root2, foldername2) parentfa = os.path.dirname(fa_file) for root3, subfolders3, filenames3 in os.walk(src): for foldername3 in subfolders3: if "HAWK" in foldername3 or "OCT" in foldername3 or "Visit" in foldername3 or "V": oct_file = os.path.join(root3, foldername3) parentoct = os.path.dirname(oct_file) for root4, subfolders4, filenames4 in os.walk(src): for foldername4 in subfolders4: if "OCTs" in foldername4: oct_dest = os.path.join(root4, foldername4) new_path = os.path.join(oct_dest, os.path.basename(parentoct)) try: shutil.move(parentoct, oct_dest) shutil.move(parentfa, other_folder) except OSError: if os.path.exists(new_path): print("file exist")
Basically I am trying to select some of those folders and move them to "OCT" folder, end move the rest to "other" folder. But the code keep looping, never stops.
I’d be glad to hear if you guys have any ideas for how to solve this problem.
And I know it’s not the cleanest code haha!
Source: Python-3x Questions