CBSE Class 12 Computer Science Question 93 of 136

Data File Handling — Question 32

Back to all questions
32
Question

Question 32

Write a function Remove_Lowercase() that accepts two file names, and copies all lines that do not start with lowercase letter from the first file into the second file.

Answer

Let the file "file1.txt" include the following sample text:

Dew on petals, morning's gift.
silent moon, silver glow.
Winds whisper, secrets shared.
rain's embrace, earth's renewal.
def Remove_Lowercase(input_file, output_file):
    input_file = open(input_file, 'r')
    output_file = open(output_file, 'w')
    for line in input_file:
        if line.strip() and line[0].isupper():
            output_file.write(line)
    input_file.close()
    output_file.close()

input_file = 'file1.txt'
output_file = 'file2.txt'
Remove_Lowercase(input_file, output_file)

The file "file2.txt" includes following text:

Dew on petals, morning's gift.
Winds whisper, secrets shared.