← All Exercises

Python File Reading

basicpython~15min

Background

File I/O is fundamental for data processing, log analysis, and configuration loading in Python applications.

Problem

Write a function that reads a file line by line, strips whitespace from each line, and returns a list of non-empty lines.

Input

A file path string.

Output

A list of stripped, non-empty lines from the file.

Sample tests

Sample 1

Input: "data.txt" with lines "a", "", "b"
Expected: ["a", "b"]

Your solution

Learning objective

Learn file reading with context managers and exception handling for missing files.