Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
75 views
in Python by (120 points)
Write a program in python that copies all the contents of the oldfile except for the words ending in "ly" into a new

Please log in or register to answer this question.

1 Answer

0 votes
by (40.9k 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():
    old_file_name = "oldfile.txt"
    new_file_name = "newfile.txt"

    try:
        with open(old_file_name, "r") as old_file:
            old_content = old_file.read()

        filtered_content = filter_ly_words(old_content)

        with open(new_file_name, "w") as new_file:
            new_file.write(filtered_content)

        print("Contents copied excluding words ending in 'ly'.")

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

if __name__ == "__main__":
    main()

Replace "oldfile.txt" with the name of your existing file and "newfile.txt" with the desired name for the new file. The program reads the content of the old file, filters out words ending in "ly", and then writes the filtered content to the new file. Make sure both the old file and the new 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 15, 2023 in Python by MAhmad (120 points)
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 (114k points)
0 votes
1 answer
0 votes
1 answer
asked Apr 4, 2023 in Python by kvdevika (114k points)

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

...