Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2026 Crash Course
NEET 2026 Crash Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
171 views
in Python by (120 points)
In python, write a program that copies all the contents of the file to another file except the words that end with "ly".

Please log in or register to answer this question.

1 Answer

0 votes
by (41.0k points)
def filter_ly_words(text):

    words = text.split()
    filtered_words = [word for word in words if not word.endswith("ly")]
    return " ".join(filtered_words)

def main():
    input_file_name = "input.txt"
    output_file_name = "output.txt"

    try:
        with open(input_file_name, "r") as input_file:
            input_content = input_file.read()
            
        filtered_content = filter_ly_words(input_content)
        
        with open(output_file_name, "w") as output_file:
            output_file.write(filtered_content)
        
        print("Contents copied excluding words ending in 'ly'.")

    except FileNotFoundError:
        print("The input file was not found.")

if __name__ == "__main__":
    main()

Replace "input.txt" with the name of the file you want to read from, and "output.txt" with the desired name for the file where the filtered content will be copied to. The program reads the content of the input file, filters out words ending in "ly", and then writes the filtered content to the output file. Just make sure both the input file and the output file are in the same directory as the script or provide the full paths to the files.

Related questions

0 votes
1 answer
asked Aug 16, 2023 in Python by MAhmad (120 points)
0 votes
1 answer
asked Apr 4, 2023 in Python by kvdevika (178k points)
0 votes
1 answer
0 votes
1 answer
asked Apr 4, 2023 in Python by kvdevika (178k points)
0 votes
1 answer

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...