Write a method in Python to read the content from a text file DIARY.TXT line by line and display the same on the screen.
Python method called read_diary_file that reads the content from a text file named “DIARY.TXT” line by line and displays it on the screen: def read_diary_file(): try: with open("DIARY.TXT", 'r') as…
Write a method in Python to write multiple line of text contents into a text file mylife.txt.
Python method called write_multiple_lines that writes multiple lines of text content into a text file named “mylife.txt”: def write_multiple_lines(contents): try: with open("mylife.txt", 'w') as file: file.writelines(contents) print("Text content written successfully…
Write a program to display all the records in a file along with line/record number
Python program that displays all the records in a file along with their line numbers: def display_records_with_line_numbers(filename): try: with open(filename, 'r') as file: for line_number, line in enumerate(file, start=1): print(f"Line…
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.
Python function called remove_lowercase that accepts two file names as arguments and copies all lines that do not start with a lowercase letter from the first file into the second…
Write a Python program to display the size of a file after removing EOL characters, leading and trailing white spaces and blank lines.
Here’s a Python program that calculates the size of a file after removing end-of-line (EOL) characters, leading and trailing white spaces, and blank lines: def calculate_file_size(filename): try: with open(filename, 'r')…
Write a program to read and display content of file from end to beginning.
Reading a file line by line from 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 accept a filename from the user and display all the lines from the file which contain Python comment character ‘#”.
Here’s a Python program that accepts a filename from the user and displays all the lines from the file that contain the Python comment character ‘#’: def display_comment_lines(filename): try: with…
Write a program to read a file ‘Story.txt’ and create another file, storing an index of Story.txt, telling which line of the file each word appears in. If word appears more than once, then index should show all the line numbers containing the word.
Here’s a Python program that reads a file called ‘Story.txt’, creates another file called ‘Index.txt’, and stores an index of the words in ‘Story.txt’, indicating the line numbers where each…
Write a function to insert a sentence in a text file, assuming that text file is very big and can’t fit in computer’s memory.
To insert a sentence into a very large text file without loading the entire file into memory, you can use a technique called “file buffering” or “file rewriting.” Here’s an…
Write a function in Python to count and display the number of lines starting with alphabet
Write a function in Python to count and display the number of lines starting with alphabet “A” present in a text file “LINES.TXT”, e.g., the file “LINE.TXT” contains the following…