Author: admin
-
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 file: for line in file: print(line.rstrip()) except FileNotFoundError: print(“DIARY.TXT file not found.”) # Example usage: read_diary_file() The read_diary_file method opens the file “DIARY.TXT” in…
-
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 to ‘mylife.txt’.”) except IOError: print(“Error occurred while writing to file.”) # Example usage: text_contents = [“This is line 1.\n”, “This is line 2.\n”, “This is…
-
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 {line_number}: {line.rstrip()}”) except FileNotFoundError: print(f”File ‘{filename}’ not found.”) filename = input(“Enter the filename: “) display_records_with_line_numbers(filename) When you run the program, it prompts you to…
-
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 file: def remove_lowercase(source_file, destination_file): try: with open(source_file, ‘r’) as file1, open(destination_file, ‘w’) as file2: for line in file1: if not line.strip().startswith(tuple(‘abcdefghijklmnopqrstuvwxyz’)): file2.write(line) print(“Lines copied successfully.”)…
-
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’) as file: modified_content = [] for line in file: line = line.strip() # Remove leading and trailing white spaces if line: # Skip blank lines…
-
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 read and display content of file from end to beginning. Reading a file backward can be achieved by using seek and…
-
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 open(filename, ‘r’) as file: for line in file: if ‘#’ in line: print(line.strip()) except FileNotFoundError: print(f”File ‘{filename}’ not found.”) filename = input(“Enter the filename: “)…
-
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 word appears: def create_word_index(input_file, output_file): word_index = {} with open(input_file, ‘r’) as file: for line_number, line in enumerate(file, start=1): words = line.strip().split() for word in…
-
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 example function in Python that demonstrates this approach: def insert_sentence(filename, sentence, line_number): temp_filename = filename + “.tmp” with open(filename, ‘r’) as original_file, open(temp_filename, ‘w’) as…
-
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 lines: A boy is playing there. There is a playground. An aeroplane is in the sky. Alphabets & numbers are allowed in password. The function…