Skip to content

Conversation

@leonlan
Copy link
Member

@leonlan leonlan commented Feb 5, 2025

"""
One-off script to merge part A and part B ASLIB files into a single file.
"""

from pathlib import Path

dirs = [f"ASLIB{idx}" for idx in range(7)]
for idx in range(7):
    dir_loc = Path(f"ASLIB{idx}")

    # Find all unique file names. We do this by finding all part A files.
    suffix = "a.RCP"
    fnames = [loc for loc in dir_loc.glob("*") if suffix in loc.name]
    unique = [loc.name.removesuffix(suffix) for loc in fnames]

    # Destination of the new files.
    new_dir = "new" / dir_loc
    new_dir.mkdir(parents=True, exist_ok=True)

    # We create a new file for each unique file name by concatenating the
    # corresponding part A and part B files.
    for name in unique:
        part1 = dir_loc / f"{name}a.RCP"
        part2 = dir_loc / f"{name}b.RCP"

        with open(part1, "r") as fh:
            lines1 = fh.readlines()

        with open(part2, "r") as fh:
            lines2 = fh.readlines()

        new_loc = new_dir / f"{name}.rcp"
        with open(new_loc, "w") as fh:
            fh.writelines(lines1)
            fh.writelines("")
            fh.writelines(lines2)

@leonlan leonlan marked this pull request as ready for review February 20, 2025 08:00
@leonlan leonlan merged commit 03d081f into main Feb 20, 2025
5 checks passed
@leonlan leonlan deleted the optional branch February 20, 2025 16:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants