0% found this document useful (0 votes)
2 views3 pages

Py99 Sam

Uploaded by

nasrinsamima742
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Py99 Sam

Uploaded by

nasrinsamima742
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

[23-10-2024]

A13. Write a Python Program to Count the Occurrences of a Word in a Text File.
Solution:
Python Code:

def count_word_occurrences(file_name, target_word):


try:
# Open the file in read mode
with open(file_name, 'r') as file:
# Read the content of the file and convert to lowercase
content = file.read().lower()

# Split the content into words


words = content.split()

# Count occurrences of the target word


count = words.count(target_word.lower())

print(f"The word '{target_word}' occurs {count} times in


the file.")

except FileNotFoundError:
print(f"Error: The file '{file_name}' was not found.")

# Example usage
file_name = 'sample.txt' # Replace with the path to your file
target_word = 'python' # Replace with the word you want to search
count_word_occurrences(file_name, target_word)

Algorithm:

1. Open the text file in read mode.

2. Read the content of the file and convert it to lowercase (to handle case insensitivity).

3. Split the text into a list of words.

4. Iterate through the list to count occurrences of the target word.

5. Display the result showing how many times the word occurs.

6. Close the file.


A14. Write a Python Program which will store selected content of an existing file to a new file.

Existing file:[Id-marks1-marks2-marks3-marks4-marks5]

110011 89 67 75 90 56

110101 78 88 50 92 72

110102 60 70 56 91 51

110201 77 88 60 71 67

110202 55 61 71 62 73

New file will contain the lines with average marks more than 65.

Solution:

Python Code:

def filter_high_average(input_file, output_file):


try:
# Open the input file in read mode
with open(input_file, 'r') as infile, open(output_file, 'w') as
outfile:
for line in infile:
# Split the line into ID and marks
parts = line.split()
if len(parts) < 6:
continue # Skip malformed lines

# Extract the marks (as integers) from the line


marks = list(map(int, parts[1:])) # From index 1
onwards are marks

# Calculate the average of the marks


average = sum(marks) / len(marks)

# If average is more than 65, write the line to the new


file
if average > 65:
outfile.write(line)

print(f"Lines with average marks greater than 65 have been


stored in '{output_file}'.")

except FileNotFoundError:
print(f"Error: The file '{input_file}' was not found.")

# Example usage
input_file = 'existing_file.txt' # Replace with your input file path
output_file = 'new_file.txt' # Replace with your output file path
filter_high_average(input_file, output_file)

Algorithm:

1. Open the existing file in read mode.

2. Create a new file to store the filtered content.

3. For each line:

o Extract the marks.

o Calculate the average of the marks.

o If the average is greater than 65, store the line in the new file.

4. Close both files after processing.

You might also like