w3resource logo


Python Exercises

Python File I/O: Remove newline characters from a file

Python File I/O: Exercise-17 with Solution

Write a Python program to remove newline characters from a file.

Sample Solution:-

Python Code:

def remove_newlines(fname):
    flist = open(fname).readlines()
    return [s.rstrip('\n') for s in flist]


Sample Output:

['Welcome to w3resource.com.', 'Append this text.Append this text.Append this text.', 'Append this text.', 'Ap
pend this text.', 'Append this text.', 'Append this text.'] 

Python Code Editor:

Improve this sample solution and post your code through Disqus