<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def write_gtfs(feed: "Feed", path: Path, ndigits: int = 6) -> None: """ Export the given feed to the given path. If the path end in '.zip', then write the feed as a zip archive. Otherwise assume the path is a directory, and write the feed as a collection of CSV files to that directory, creating the directory if it does not exist. Round all decimals to ``ndigits`` decimal places. All distances will be the distance units ``feed.dist_units``. """

    path = Path(path)

    if path.suffix == ".zip":
        # Write to temporary directory before zipping
        zipped = True
        tmp_dir = tempfile.TemporaryDirectory()
        new_path = Path(tmp_dir.name)
    else:
        zipped = False
        if not path.exists():
            path.mkdir()
        new_path = path

    for table in cs.GTFS_REF["table"].unique():
        f = getattr(feed, table)
        if f is None:
            continue

        f = f.copy()
        # Some columns need to be output as integers.
        # If there are NaNs in any such column,
        # then Pandas will format the column as float, which we don't want.
        f_int_cols = set(cs.INT_COLS) & set(f.columns)
        for s in f_int_cols:
            f[s] = f[s].fillna(-1).astype(int).astype(str).replace("-1", "")
        p = new_path / (table + ".txt")
        f.to_csv(str(p), index=False, float_format=f"%.{ndigits}f")

    # Zip directory
    if zipped:
        basename = str(path.parent / path.stem)
        shutil.make_archive(basename, format="zip", root_dir=tmp_dir.name)
        tmp_dir.cleanup()