Text File Statistics

Easy ⏱ 8 min 75% acceptance ★★★★★ 4.9
Write file_stats(path) returning a dict with keys lines, words, and chars — the classic wc utility. Words split on whitespace; chars count everything including newlines.

Examples

Example 1
Input
file_stats('note.txt')  # 'hi there\nbye\n'
Output
{'lines': 2, 'words': 3, 'chars': 13}
Explanation

Two lines, three words, thirteen characters.

Constraints

  • Count newline characters in chars.
  • Read the file once.

Topics

File Handlingcounting

Companies

TCSCognizantCapgemini

Hints

Hint 1

Read the whole text, then derive all three numbers.

Hint 2

splitlines() for lines, split() for words, len() for chars.

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