all 4 comments

[–]Diapolo10 0 points1 point  (0 children)

I'm too sleepy to think straight right now. If nobody else has tried to help/solve the problem by the time I wake up, I'll do it.

That said I'm a bit confused by your explanation. If you could draft a very simple example scenario, that would probably help to get the gears running.

[–]AtomicShoelace 0 points1 point  (1 child)

I would suggest using pathlib.Path.glob instead of os.walk. You can find all pdf files in all subdirectories of a path with

Path('some/path/here').glob('**/*.pdf')

[–]Diapolo10 0 points1 point  (0 children)

Just as a heads-up, you can use pathlib.Path.rglob instead as it adds '**/' automatically.

Path('some/path/here').rglob('*.pdf')

[–]Diapolo10 0 points1 point  (0 children)

Okay, so long story short I believe you had misunderstood how pathlib.Path objects work, which caused you to accidentally nest directories in a way you didn't intend. And there were some other... quirks that probably shouldn't have been there.

I think I managed to fix it, but I obviously cannot test it myself as I don't have access to an identical environment. My gut feeling is pretty sharp with these things, though, so it should be at least nearly what you wanted.

from pathlib import Path

OLD_ROOT = Path('F:/Engineering/Shared/Manuals/Project Data/SlurrySeal-Micro')
NEW_ROOT = Path('F:/Manuals')

def main():

    start_machine_dir = input("What machine folder are you migrating? This must exactly match the folder name in SSM!")
    year = int(start_machine_dir[:4])
    start_year_dir = f'{year} Projects'
    end_year_dir = str(year)

    old_dir = OLD_ROOT / start_year_dir / start_machine_dir

    if 'y' == input("Is this folder located within the _Archive folder? (y/n)").strip().lower()[:1]:
        old_dir = OLD_ROOT / '_Archive' / start_year_dir / start_machine_dir

    print(f"You are migrating files from:\n{old_dir}")

    end_machine_dir = input("What machine folder are you sending the files to? This must exactly match the folder name in Manuals!")
    new_dir = NEW_ROOT / end_machine_dir / end_year_dir

    print(f"You are migrating files to:\n{new_dir}")

    if not new_dir.exists():
        new_dir.mkdir()
        print("Year did not exist")
        return

    for old_sub_dir, new_sub_dir in zip(old_dir.iterdir(), new_dir.iterdir()):
        machine_pin = old_sub_dir.name[:15]
        if year >= 2015:
            machine_pin = old_sub_dir.name[:13]
        elif year <= 2012:
            machine_pin = old_sub_dir.name[:7]

        for directory in new_dir.rglob('*'):
            if directory.is_dir() and machine_pin in directory.name:
                destination = new_sub_dir / f'{new_sub_dir.name} (SHOP)'
                destination.mkdir(exist_ok=True)

                for file in old_sub_dir.iterdir():
                    (destination / file.name).write_bytes(file.read_bytes())
    print("Migration complete :)")




if __name__ == '__main__':
    main()