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

Is a file closed?

Is a file closed?

EasyFile Handling Programs · 23 of 38

Write a Python program to check whether a file is closed.

Output
False
True
inside with: False
after with: True

Approach

  1. The closed attribute.

Solution

Python
f = open("temp.txt", "w")print(f.closed)f.close()print(f.closed) with open("temp.txt") as g:    print("inside with:", g.closed)print("after with:", g.closed)
Output
False
True
inside with: False
after with: True

Learn the concept

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

More file handling programs

← All 38 File Handling Programs

Write a tuple to a file

Write a tuple to a file

EasyFile Handling Programs · 22 of 38

Write a Python program to write a tuple to a file and read it back.

Output
(12, 45, 78) True

Approach

  1. Write one item per line; convert back when reading.

Solution

Python
data = (12, 45, 78)with open("tuple.txt", "w") as f:    f.write("\n".join(map(str, data))) with open("tuple.txt") as f:    loaded = tuple(int(line) for line in f)print(loaded, loaded == data)
Output
(12, 45, 78) True

Learn the concept

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

More file handling programs

← All 38 File Handling Programs

Size of a file

Size of a file

EasyFile Handling Programs · 21 of 38

Write a Python program to get the size of a file in bytes.

Output
1500 bytes

Approach

  1. os.path.getsize().

Solution

Python
import os with open("data.bin", "wb") as f:    f.write(b"x" * 1500) print(os.path.getsize("data.bin"), "bytes")
Output
1500 bytes

Line by line

Line 3

Writing in binary mode makes the size exact on every operating system.

Learn the concept

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

More file handling programs

← All 38 File Handling Programs

Count lines in a file

Count lines in a file

EasyFile Handling Programs · 20 of 38

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

Output
4

Approach

  1. sum(1 for _ in f) counts without storing the lines.

Solution

Python
with open("story.txt", "w") as f:    f.write("a\nb\nc\nd\n") with open("story.txt") as f:    print(sum(1 for _ in f))
Output
4

Learn the concept

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

More file handling programs

← All 38 File Handling Programs

Compare two files

Compare two files

EasyFile Handling Programs · 19 of 38

Write a Python program to compare two files and show the lines that differ.

Output
Same: False
--- a.txt
+++ b.txt
@@ -2 +2 @@
-line two
+line 2

Approach

  1. filecmp.cmp for equal/not equal; difflib to show differences.

Solution

Python
import difflibimport filecmp with open("a.txt", "w") as f:    f.write("line one\nline two\nline three\n")with open("b.txt", "w") as f:    f.write("line one\nline 2\nline three\n") print("Same:", filecmp.cmp("a.txt", "b.txt", shallow=False))with open("a.txt") as a, open("b.txt") as b:    for line in difflib.unified_diff(a.readlines(), b.readlines(), "a.txt", "b.txt", n=0):        print(line.rstrip("\n"))
Output
Same: False
--- a.txt
+++ b.txt
@@ -2 +2 @@
-line two
+line 2

Line by line

Line 9

shallow=False compares the contents, not just size and date.

Line 11

A unified diff shows removed lines with - and added lines with +.

Learn the concept

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

More file handling programs

← All 38 File Handling Programs