Python Practice Lab
mediumUnit 10 · Modules, Exception Handling & File Handling

P30. File Word Count

Modules, Exception Handling & File Handling

Read n lines of text and write them to a file named notes.txt. Then open the file again in read mode and count its lines, words and characters (not counting the newline at the end of each line). Also print the longest line (the first one if there is a tie). Use with open(...) for both steps.

Input Format

  • Line 1: n
  • Next n lines: the text

Output Format

  • Line 1: Lines: <count>
  • Line 2: Words: <count>
  • Line 3: Characters: <count>
  • Line 4: Longest line: <line>

Constraints

  • 1 ≤ n ≤ 50
  • Each line has 1 to 100 characters

Sample Test Cases

Sample 1
Input
3
Python is easy
Files store data
Practice daily
Output
Lines: 3
Words: 8
Characters: 44
Longest line: Files store data
Sample 2
Input
1
Hello
Output
Lines: 1
Words: 1
Characters: 5
Longest line: Hello

Submitting checks 2 sample cases and 2 hidden test cases.

Hints

Saved
Custom 1

This is your own input, so Run will show what your program prints but can't say whether it's correct.