Characters of a file into a list

EasyFile Handling Programs · 24 of 38

Write a Python program to extract the characters (not spaces or newlines) of a file into a list.

Advertisement
Output
['s', 'q', 'a', 't', 'o', 'o', 'l', 's', 'p', 'y']

Approach

  1. Keep characters that are not whitespace.

Solution

Python
with open("word.txt", "w") as f:    f.write("sqa tools\npy\n") with open("word.txt") as f:    print([ch for ch in f.read() if not ch.isspace()])
Output
['s', 'q', 'a', 't', 'o', 'o', 'l', 's', 'p', 'y']
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More file handling programs

← All 38 File Handling Programs