Read the Non-Blank Lines

Easy ⏱ 8 min 78% acceptance ★★★★★ 4.6
Config files often contain blank lines and stray whitespace. Write clean_lines(path) returning a list of every line with surrounding whitespace stripped, skipping lines that are empty after stripping.

Examples

Example 1
Input
clean_lines('conf.txt')  # file: 'host=a\n\n  port=80  \n'
Output
['host=a', 'port=80']
Explanation

The blank middle line is dropped; the others are stripped.

Constraints

  • Strip each line.
  • Skip whitespace-only lines.

Topics

File Handlingcleanup

Companies

CapgeminiHCLTech

Hints

Hint 1

line.strip() both cleans and tests emptiness.

Hint 2

A comprehension over the file object works.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.