Untitled

 avatar
unknown
plain_text
a year ago
775 B
10
Indexable
def fast_copy_if_not_exists(src_dir: str, dst_dir: str):
    """
    Faster version: pre-build a set of destination filenames and copy only missing ones.
    """
    if not os.path.exists(src_dir):
        raise FileNotFoundError(f"Source directory '{src_dir}' does not exist.")

    os.makedirs(dst_dir, exist_ok=True)

    src_files = os.listdir(src_dir)
    dst_files = set(os.listdir(dst_dir))  # load once into memory

    for file_name in tqdm(src_files, desc="Copying files", unit="file"):
        if file_name not in dst_files:  # O(1) lookup
            src_path = f"{src_dir}/{file_name}"
            dst_path = f"{dst_dir}/{file_name}"

            if os.path.isfile(src_path):
                shutil.copy(src_path, dst_path)  # faster than copy2
Editor is loading...
Leave a Comment