Python program to Reverse a single line of a text file Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report Given a text file. The task is to reverse a single line of user's choice from a given text file and update the already existing file. Examples: Input: Hello Geeks for geeks! User choice = 1 Output: Hello Geeks geeks! for Input: This is a geek Welcome to GeeksforGeeks GeeksforGeeks is a computer science portal User choice = 0 Output: geek a is This Welcome to GeeksforGeeks GeeksforGeeks is a computer science portal Implementation: Let's suppose the text file looks like this - Python3 # Open file in read mode f = open('GFG.txt', 'r') # Read the content of the # file and store it in a list lines = f.readlines() # Close file f.close() # User's choice choice = 1 # Split the line into words line = lines[choice].split() # line is reversed Reversed = " ".join(line[::-1]) # Updating the content of the # file lines.pop(choice) lines.insert(choice, Reversed) # Open file in write mode u = open('GFG.txt', 'w') # Write the new content in file # and note, it is overwritten u.writelines(lines) u.close() Output: Time complexity: O(n), where n is the number of lines in the file. Auxiliary space: O(n), where n is the number of lines in the file. Comment More infoAdvertise with us Next Article Company-wise Practice Problems R riasehgal1999 Follow Improve Article Tags : Python Python Programs python-file-handling Python file-handling-programs Practice Tags : python Similar Reads Interview PreparationInterview Preparation For Software DevelopersMust Coding Questions - Company-wise Must Do Coding Questions - Topic-wiseCompany-wise Practice ProblemsCompany PreparationCompetitive ProgrammingSoftware Design-PatternsCompany-wise Interview ExperienceExperienced - Interview ExperiencesInternship - Interview ExperiencesPractice @GeeksforgeeksProblem of the DayTopic-wise PracticeDifficulty Level - SchoolDifficulty Level - BasicDifficulty Level - EasyDifficulty Level - MediumDifficulty Level - HardLeaderboard !!Explore More...Data StructuresArraysLinked ListStackQueueBinary TreeBinary Search TreeHeapHashingGraphAdvance Data StructuresMatrixStringAll Data StructuresAlgorithmsAnalysis of AlgorithmsSearching AlgorithmsSorting AlgorithmsPattern SearchingGeometric AlgorithmsMathematical AlgorithmsRandomized AlgorithmsGreedy AlgorithmsDynamic ProgrammingDivide & ConquerBacktrackingBranch & BoundAll AlgorithmsProgramming LanguagesCC++JavaPythonC#Go LangSQLPHPScalaPerlKotlinWeb TechnologiesHTMLCSSJavaScriptBootstrapTailwind CSSAngularJSReactJSjQueryNodeJSPHPWeb DesignWeb BrowserFile FormatsComputer Science SubjectsOperating SystemsDBMSComputer NetworkComputer Organization & ArchitectureTOCCompiler DesignDigital Elec. & Logic DesignSoftware EngineeringEngineering MathematicsData Science & MLComplete Data Science CourseData Science TutorialMachine Learning TutorialDeep Learning TutorialNLP TutorialMachine Learning ProjectsData Analysis TutorialTutorial LibraryPython TutorialDjango TutorialPandas TutorialKivy TutorialTkinter TutorialOpenCV TutorialSelenium TutorialGATE CSGATE CS NotesGate CornerPrevious Year GATE PapersLast Minute Notes (LMNs)Important Topic For GATE CSGATE CoursePrevious Year Paper: CS exams Like