def write_events(filename, rows): staging_file = BASE / f".{filename}.tmp" with staging_file.open("w", encoding="utf-8") as handle: for row in rows: handle.write(json.dumps(row, sort_keys=True) + "\n") staging_file.replace(INPUT / filename) write_events( "events-001.json", [ {"event": "checkout", "region": "MY", "count": 3}, {"event": "search", "region": "SG", "count": 2}, ], )