How to Remove Newline Characters from a text File?

In this article, we will show you how to remove the newline character(\n) from a given text file using python.

Assume we have taken a text file with the name TextFile.txt consisting of some random text. We will remove the newline character(\n) from a given text file.


Good Morning TutorialsPoint
This is TutorialsPoint sample File
Consisting of Specific
source codes in Python,Seaborn,Scala
Summary and Explanation
Welcome TutorialsPoint
Learn with a joy

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the path of the text file.

  • Use the open() function(opens a file and returns a file object as a result) to open the text file in read-only mode by passing the file name, and mode as arguments to it (Here “r” represents read-only mode).

with open(inputFile, 'r') as filedata:
  • Using the readlines() function (returns a list with each line in the file represented as a list item. To limit the number of lines returned, use the hint argument. No more lines are returned if the total amount of bytes returned exceeds the specified number) to obtain the list of lines of a given input text file with a new line character (\n) at the end.

  • Use the rstrip() function(removes any trailing characters i.e, characters present at the end of a string. The default trailing character to remove is space) and list comprehension(here we are iterating in each line of the list using the for loop), to remove the newline character(\n) from the above list of lines of a text file and print them.

list comprehension:
When you wish to build a new list based on the values of an existing list, list comprehension provides a shorter/concise syntax.
  • Close the input file with the close() function(used to close an opened file).


The following program checks line by line if the given word is found in a line from a text file and prints the line if the word is found −

# input text file inputFile = "ExampleTextFile.txt" # Opening the given file in read-only mode with open(inputFile, 'r') as filedata: # Reading the file lines using readlines() linesList= filedata.readlines() # Removing the new line character(\n) from the list of lines print([k.rstrip('\n') for k in linesList]) # Closing the input file filedata.close()


On executing, the above program will generate the following output −

['Good Morning TutorialsPoint', 'This is TutorialsPoint sample File', 'Consisting of Specific', 'source codes in Python, Seaborn,Scala', 'Summary and Explanation', 'Welcome TutorialsPoint', 'Learn with a joy']

We gave our program a text file containing some random content and then opened it in reading mode. The readlines() function was then used to retrieve a list of all the lines in the file. Using the list comprehension, we went over each line of the file and deleted the newline character with the rstrip() method. Finally, we closed the file by printing the updated lines without the new line characters.

So, from this article, we learned how to open a file and read lines from it, which can be used to execute operations such as finding the number of words in a line, the length of a line, and so on. We also learned how to use list comprehension for precise and simple code, as well as how to delete newline characters from every line of the file. This method may also be used to remove any specific letters/words from the file's lines.

Updated on: 17-Aug-2022

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started