Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can I process several selected text files without changing their originals?

Quick answer

Put only the files you intend to check in an explicit path list, then open each one in read mode and collect its result separately. For CSV files, Python’s csv module uses open(..., newline='') with csv.reader or csv.DictReader [S1]. Catch FileNotFoundError and record that path as missing—not as a successful check. This example keeps results in memory and does not write to the input files.

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

How can I run the same read-only check on several selected CSV files without changing the originals?

Geez

Keep the input selection narrow: write down the exact paths you want to process rather than scanning a folder. Open each selected file in read mode, perform the same check, and save the outcome in a separate results list. The example below counts records yielded by the CSV reader and records a missing path distinctly. It does not create or modify files; results exist only in memory until your program ends.

import csv

paths = [
    "samples/january.csv",
    "samples/february.csv",
    "samples/march.csv",
]

results = []

for path in paths:
    try:
        with open(path, "r", newline="") as file:
            row_count = sum(1 for _ in csv.DictReader(file))
        results.append({"path": path, "status": "ok", "rows": row_count})
    except FileNotFoundError:
        results.append({"path": path, "status": "missing"})

for result in results:
    print(result)

Python’s documented CSV pattern opens a named input with open(..., newline='') and reads it with csv.reader or csv.DictReader [S1]. The explicit path list, result collection, and missing-file status are application logic built around that pattern. Replace the sample paths with your own selected paths.

Follow-up question

What does the row count include, and how can I change the check?

Geez

Here, the count is the number of records yielded by csv.DictReader(file). To use a different repeatable check, replace the sum(...) expression with your own logic while leaving the path loop and result handling intact. Decide what counts as a passing result before processing the batch, and make that outcome explicit in each result object. The example is intentionally simple: it does not validate whether the files have the right columns or whether their contents are meaningful.

Follow-up question

How do I make sure a missing file is not counted as a successful result?

Geez

The try block adds an ok result only after the file has been opened and the check has completed. If opening the selected path raises FileNotFoundError, the except block adds a missing result instead, with no row count. That keeps the status visible without pretending the file contained zero records. When you review the printed results, count only entries whose status is ok; investigate or correct paths marked missing before relying on a complete batch. The example handles missing paths specifically; it does not label every other possible problem as “missing.”

Follow-up question

Can I save the results separately while still leaving the selected inputs alone?

Geez

Yes. The shown version keeps results in the results list and prints them; it never opens an input for writing. If you later want a report file, choose a separate output path that is not one of the selected inputs, and write the results there deliberately. This example does not add that step, so it does not write a report file. Before running, check that the path list contains only intended inputs and that any output destination you add is distinct.

A practical checklist

  • List only the exact CSV paths you intend to check.

  • Open each selected input with mode "r"; use newline="" for the CSV reader pattern.

  • Keep results in a separate list or a deliberately separate output destination.

  • Record a missing path with a distinct status, not a zero-record success.

  • Review all statuses, and investigate missing or otherwise unhandled errors separately.

A few more useful answers

Does this example change the original CSV files?

No. It opens each input in read mode and only stores results in memory. The example does not write to any file.

What happens if one selected path does not exist?

That path receives a missing status through FileNotFoundError; it is not given an ok status or a row count.

Does this check prove that each CSV is valid for my task?

No. It counts records yielded by DictReader, but does not establish that required columns, values, or other task-specific rules are correct. Add checks that match your intended use.

Sources

References used for this guide.

  1. [S1] Python 3.14 csv module: reading a selected CSV file
    docs.python.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work