Count consonants in a file

Count consonants in a file

EasyFile Handling Programs · 35 of 38

Write a Python program to count the consonants in a file.

Output
16

Approach

  1. Letters that are not vowels.

Solution

Python
with open("story.txt", "w") as f:    f.write("Python file handling is easy\n") with open("story.txt") as f:    text = f.read().lower()print(sum(ch.isalpha() and ch not in "aeiou" for ch in text))
Output
16

Learn the concept

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

More file handling programs

← All 38 File Handling Programs

Count vowels in a file

Count vowels in a file

EasyFile Handling Programs · 34 of 38

Write a Python program to count the vowels in a file.

Output
8

Approach

  1. Count characters in “aeiou” after lower-casing.

Solution

Python
with open("story.txt", "w") as f:    f.write("Python file handling is easy\n") with open("story.txt") as f:    print(sum(ch in "aeiou" for ch in f.read().lower()))
Output
8

Learn the concept

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

More file handling programs

← All 38 File Handling Programs

Read a file in reverse

Read a file in reverse

EasyFile Handling Programs · 33 of 38

Write a Python program to print the lines of a file in reverse order, and the whole text reversed.

Output
third
second
first
driht
dnoces
tsrif

Approach

  1. reversed(lines) for line order; [::-1] for characters.

Solution

Python
with open("story.txt", "w") as f:    f.write("first\nsecond\nthird\n") with open("story.txt") as f:    lines = f.read().splitlines()for line in reversed(lines):    print(line)print("\n".join(lines)[::-1])
Output
third
second
first
driht
dnoces
tsrif

Learn the concept

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

More file handling programs

← All 38 File Handling Programs

Move the cursor with seek()

Move the cursor with seek()

EasyFile Handling Programs · 32 of 38

Write a Python program to move the cursor to a specific position in a file.

Output
Python
Hello

Approach

  1. seek(n) moves to byte n; reading continues from there.

Solution

Python
with open("story.txt", "w") as f:    f.write("Hello Python\n") with open("story.txt") as f:    f.seek(6)    print(f.read().strip())    f.seek(0)    print(f.read(5))
Output
Python
Hello

Learn the concept

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

More file handling programs

← All 38 File Handling Programs

Cursor position with tell()

Cursor position with tell()

EasyFile Handling Programs · 31 of 38

Write a Python program to find the current cursor position in a file.

Output
0
5

Approach

  1. tell() returns the position in bytes from the start.

Solution

Python
with open("story.txt", "w") as f:    f.write("Hello Python\n") with open("story.txt") as f:    print(f.tell())    f.read(5)    print(f.tell())
Output
0
5

Learn the concept

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

More file handling programs

← All 38 File Handling Programs

Count special characters in a file

Count special characters in a file

EasyFile Handling Programs · 30 of 38

Write a Python program to count special characters (not letters, digits or whitespace) in a file.

Output
5

Approach

  1. Count characters that are neither alphanumeric nor whitespace.

Solution

Python
with open("story.txt", "w") as f:    f.write("Email: asha@sqatools.in! #python\n") with open("story.txt") as f:    print(sum(1 for ch in f.read() if not ch.isalnum() and not ch.isspace()))
Output
5

Line by line

Line 5

The special characters are :, @, ., ! and #.

Learn the concept

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

More file handling programs

← All 38 File Handling Programs

Count digits in a file

Count digits in a file

EasyFile Handling Programs · 29 of 38

Write a Python program to count the digits in a file.

Output
12

Approach

  1. sum(ch.isdigit() for ch in text).

Solution

Python
with open("story.txt", "w") as f:    f.write("Order 4521 shipped on 2026-11-02\n") with open("story.txt") as f:    print(sum(ch.isdigit() for ch in f.read()))
Output
12

Learn the concept

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

More file handling programs

← All 38 File Handling Programs

Count lowercase letters in a file

Count lowercase letters in a file

EasyFile Handling Programs · 28 of 38

Write a Python program to count lowercase letters in a file.

Output
14

Approach

  1. sum(ch.islower() for ch in text).

Solution

Python
with open("story.txt", "w") as f:    f.write("Learn Python At SQA Tools\n") with open("story.txt") as f:    print(sum(ch.islower() for ch in f.read()))
Output
14

Learn the concept

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

More file handling programs

← All 38 File Handling Programs

Count uppercase letters in a file

Count uppercase letters in a file

EasyFile Handling Programs · 27 of 38

Write a Python program to count uppercase letters in a file.

Output
7

Approach

  1. sum(ch.isupper() for ch in text).

Solution

Python
with open("story.txt", "w") as f:    f.write("Learn Python At SQA Tools\n") with open("story.txt") as f:    print(sum(ch.isupper() for ch in f.read()))
Output
7

Learn the concept

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

More file handling programs

← All 38 File Handling Programs

Count characters in a file

Count characters in a file

EasyFile Handling Programs · 26 of 38

Write a Python program to count the characters in a file, with and without whitespace.

Output
All characters: 21
Without whitespace: 16

Approach

  1. len(text) and a count of non-whitespace characters.

Solution

Python
with open("story.txt", "w") as f:    f.write("Python is fun\nTry it\n") with open("story.txt") as f:    text = f.read()print("All characters:", len(text))print("Without whitespace:", sum(1 for ch in text if not ch.isspace()))
Output
All characters: 21
Without whitespace: 16

Learn the concept

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

More file handling programs

← All 38 File Handling Programs