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.