Код: Выделить всё
import shutil
import os
from pathlib import Path
source = '/Users/miram/Library/CloudStorage/OneDrive-Company/Docs/Test_SRCDirectory'
dest = '/MAIN/Building-D1D-RFS2'
fileword_target = "CRATE"
#File Rename
for file in Path(source).iterdir():
name = file.stem.split(fileword_target)[0].rstrip()
subdir = Path(dest, name)
print(f"Checking subdir: {subdir}")
#Move file into folder that matches name variable
if subdir.exists():
destination = Path(subdir, file.name)
shutil.move(file, destination)
print(f"{name} have been moved.")
else:
print(f"Folder not found for {name}")
имя файла L1 CRATE D1D
который .stem/.strip должен дать мне имя моей подпапки, которую я ищу в папке назначения.
Подробнее здесь: https://stackoverflow.com/questions/791 ... does-exist
Мобильная версия