Computer Science
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.
Related Questions
Reading a file line by line from the beginning is a common task. What if you want to read a file backward ? This happens when you need to read log files. Write a program to read and display content of file from end to beginning.
Write a Python program to display the size of a file after removing EOL characters, leading and trailing white spaces and blank lines.
Write a program to display all the records in a file along with line/record number.
Write a method in Python to write multiple line of text contents into a text file "mylife.txt".