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

Merge two files

Merge two files

EasyFile Handling Programs · 25 of 38

Write a Python program to read two files and write their contents into a new file.

Output
Hello from part 1
Hello from part 2

Approach

  1. Open the output once and copy each input into it.

Solution

Python
with open("part1.txt", "w") as f:    f.write("Hello from part 1\n")with open("part2.txt", "w") as f:    f.write("Hello from part 2\n") with open("merged.txt", "w") as out:    for name in ["part1.txt", "part2.txt"]:        with open(name) as f:            out.write(f.read())print(open("merged.txt").read())
Output
Hello from part 1
Hello from part 2

Learn the concept

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

More file handling programs

← All 38 File Handling Programs

Characters of a file into a list

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.

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']

Learn the concept

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

More file handling programs

← All 38 File Handling Programs