Sort lines by length

Sort lines by length

EasyFile Handling Programs · 15 of 38

Write a Python program to sort the lines of a file by their length.

Output
x
short
medium line
a much longer line here

Approach

  1. sorted(lines, key=len).

Solution

Python
with open("story.txt", "w") as f:    f.write("medium line\nx\na much longer line here\nshort\n") with open("story.txt") as f:    lines = f.read().splitlines()for line in sorted(lines, key=len):    print(line)
Output
x
short
medium line
a much longer line here

Learn the concept

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

More file handling programs

← All 38 File Handling Programs

Replace spaces with underscores

Replace spaces with underscores

EasyFile Handling Programs · 38 of 38

Write a Python program to replace every space in a file with an underscore.

Output
learn_python_at_sqatools
file_handling_demo

Approach

  1. Read, replace, write back.

Solution

Python
with open("story.txt", "w") as f:    f.write("learn python at sqatools\nfile handling demo\n") with open("story.txt") as f:    text = f.read()with open("story.txt", "w") as f:    f.write(text.replace(" ", "_"))print(open("story.txt").read())
Output
learn_python_at_sqatools
file_handling_demo

Line by line

Lines 4-7

The file is read completely before it is reopened in “w” mode, because “w” empties it immediately.

Learn the concept

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

More file handling programs

← All 38 File Handling Programs

Words shorter than 5 characters

Words shorter than 5 characters

EasyFile Handling Programs · 37 of 38

Write a Python program to display the words of a file that have fewer than 5 characters.

Output
['is', 'a', 'very', 'good', 'tool', 'for', 'test']

Approach

  1. Filter words by len(w) < 5.

Solution

Python
with open("story.txt", "w") as f:    f.write("Python is a very good tool for test automation\n") with open("story.txt") as f:    print([w for w in f.read().split() if len(w) < 5])
Output
['is', 'a', 'very', 'good', 'tool', 'for', 'test']

Learn the concept

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

More file handling programs

← All 38 File Handling Programs

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