Author: admin
-
Write a program to search the names and addresses of persons having age more than 30 in the data list of persons.
Python program that searches for names and addresses of persons with an age greater than 30 in a data list of persons: def search_persons(data_list): result = [] for person in data_list: if person[‘age’] > 30: result.append((person[‘name’], person[‘address’])) return result # Example data list of persons persons = [ {‘name’: ‘John Doe’, ‘age’: 25, ‘address’: ‘123…
-
Write a program that reads characters from the keyboard one by one. All lower case characters get stored inside the file LOWER, all upper case characters get stored inside the file UPPER and all other characters get stored inside file OTHERS.
Here’s a Python program that reads characters from the keyboard one by one and stores them in separate files based on their case: lower_file = ‘LOWER.txt’ upper_file = ‘UPPER.txt’ others_file = ‘OTHERS.txt’ try: with open(lower_file, ‘w’) as lower: with open(upper_file, ‘w’) as upper: with open(others_file, ‘w’) as others: while True: char = input(“Enter a character…
-
Write a program that appends the contents of one file to another. Have the program take the filenames from the user.
Here’s a Python program that allows the user to enter the names of the source file and the destination file, and then appends the contents of the source file to the destination file: def append_file(source_file, destination_file): try: with open(source_file, ‘r’) as source: with open(destination_file, ‘a’) as destination: destination.write(source.read()) print(f”Contents of ‘{source_file}’ appended to ‘{destination_file}’.”) except…
-
Write a program that copies one file to another. Have the program read the file names from user
Here’s a Python program that allows the user to enter the names of the source file and the destination file, and then copies the contents of the source file to the destination file: def copy_file(source_file, destination_file): try: with open(source_file, ‘r’) as source: with open(destination_file, ‘w’) as destination: destination.write(source.read()) print(f”File ‘{source_file}’ successfully copied to ‘{destination_file}’.”) except…
-
Write a program to count the number of uppercase alphabets present in a text file “Poem.txt”
Here’s a Python program that counts the number of uppercase alphabets in a text file called “Poem.txt”: def count_uppercase(filename): try: with open(filename, ‘r’) as file: content = file.read() uppercase_count = 0 for char in content: if char.isupper(): uppercase_count += 1 return uppercase_count except FileNotFoundError: print(f”File ‘{filename}’ not found.”) return None filename = ‘Poem.txt’ result =…
-
Write a program to count the words “to” and “the” present in a text file “Poem.txt”
Here’s a Python program to counts the occurrences of the words “to” and “the” in a text file called “Poem.txt”: def count_words(filename): try: with open(filename, ‘r’) as file: content = file.read() # Convert all words to lowercase for case-insensitive matching content = content.lower() # Remove punctuation marks content = content.replace(‘.’, ”).replace(‘,’, ”).replace(‘!’, ”).replace(‘?’, ”) word_count…
-
A file sports.dat contains information in following format: Event-Participant
Write a function that would read contents from file sports.dat and creates a file named Atheletic.dat copying only those records from sports.dat where the event name is “Athletics”. Here’s a function that reads the contents from the file “sports.dat” and creates a file named “Athletic.dat” by copying only those records where the event name…
-
Write a code snippet that will create an object called fileout for writing; associate it with the filename STRS. The code should keep on writing strings to it as long as the user wants.
Here’s a code snippet that creates an object called fileout for writing and associates it with the filename “STRS”. The code continues writing strings to the file as long as the user wants: fileout = open(“STRS”, “w”) keep_writing = True while keep_writing: string = input(“Enter a string (or ‘q’ to quit): “) if string.lower() ==…
-
Write a program that reads a text file and creates another file that is identical except that every sequence of consecutive blank spaces is replaced by a single space
Here’s a program that reads a text file and creates another file that is identical except that every sequence of consecutive blank spaces is replaced by a single space: def remove_extra_spaces(input_file, output_file): with open(input_file, ‘r’) as file_in: with open(output_file, ‘w’) as file_out: for line in file_in: line = ‘ ‘.join(line.split()) file_out.write(line + ‘\n’) input_file =…
-
Ch-4 Data File Handling Preeti Arora Solution Computer Science
Here is the complete Preeti Arora Solution for Class 12 Computer Science for Chapter Data File Handling Unsolved Question From the Book Computer Science with Python written by Preeti Arora Q 1.What is the difference between “w” and “a” modes? Ans: The difference between “w” and “a” modes in file handling in Python is as follows:…