11 November 2026

Python Files, Exceptions, Testing and Debugging

  Asp.Net CS By Example       11 November 2026

Python 3 beginner course · Chapter 6 of 10

This chapter explains Files, Exceptions, Testing and Debugging in plain language. Read the concept, run the example, check the result and change one part to learn by doing. Optional packages are identified in their sections.

Python Files, Exceptions, Testing and Debugging concept diagramRead and writeHandle failuresTest behavior
Follow these three steps, then try the examples.

File handling with pathlib and with

Use pathlib.Path for paths and with for automatic closing. Specify an encoding for text files. Writing with write_text replaces existing content, so practice on a disposable file first.

Python example

from pathlib import Path
path = Path("note.txt")
path.write_text("Hello", encoding="utf-8")
print(path.read_text(encoding="utf-8"))

Expected result: Hello

Practice: Predict the result before running the example. Change one value or line and explain the new result.

CSV and JSON files

CSV stores rows of text fields; JSON stores nested values. Use csv and json rather than splitting strings by commas or building JSON by hand. The blog also has a full CSV student-scores tutorial linked from the course page.

Python example

import json
record = {"name": "Asha", "score": 80}
print(json.dumps(record))

Expected result: A JSON object containing name and score.

Practice: Predict the result before running the example. Change one value or line and explain the new result.

Exception handling

A try block runs code that may fail. except handles a specific error; avoid hiding every error with a bare except. Use finally for cleanup when needed, and read the exception message while debugging.

Python example

try:
    number = int("oops")
except ValueError:
    print("Enter a whole number")

Expected result: Enter a whole number

Practice: Predict the result before running the example. Change one value or line and explain the new result.

Testing and debugging

A test checks behavior against an expected result. Start with assert for tiny experiments, then use unittest for repeatable tests. When a test fails, inspect the traceback and reproduce the smallest failing case.

Python example

def double(n):
    return n * 2

assert double(4) == 8
print("Test passed")

Expected result: Test passed

Practice: Predict the result before running the example. Change one value or line and explain the new result.

Quick review

Explain the main idea to a friend in one sentence. Then recreate one example without looking at the code. If it fails, read the error message and compare the indentation and names.

Continue through the complete Python learning path. These are original explanations and diagrams; the topic checklist was compared with the w3resource Python index and the official Python tutorial.

logoblog

Thanks for reading Python Files, Exceptions, Testing and Debugging

Previous
« Prev Post

No comments:

Post a Comment

Please do not enter any spam link in the comment box.