From 0cf6b0eafd3ae93da6d3d1232d9e515568a06b10 Mon Sep 17 00:00:00 2001 From: Ritabrata Dey <82272821+Ritax2003@users.noreply.github.com> Date: Sun, 15 Oct 2023 08:08:28 +0530 Subject: [PATCH] Create Reverse-words-in-file.py Python code to reverse words in a file. File is given as the location if the file. --- R/Reverse Word in a File/Reverse-words-in-file.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 R/Reverse Word in a File/Reverse-words-in-file.py diff --git a/R/Reverse Word in a File/Reverse-words-in-file.py b/R/Reverse Word in a File/Reverse-words-in-file.py new file mode 100644 index 00000000..6a7fdce7 --- /dev/null +++ b/R/Reverse Word in a File/Reverse-words-in-file.py @@ -0,0 +1,13 @@ +filename = input("Enter the filename to read: ") +output_filename = input("Enter the filename to write: ") + +with open(filename) as file: + text = file.read() + +words = text.split() + +with open(output_filename, "w") as file: + for word in words[::-1]: + file.write(word + " ") + +print("Words in reverse order written to file successfully.")