I am using a Python script to find and replace certain strings in text files of a given directory. I am using the fileinput module to ease the find-and-replace operation, i.e., the file is read, text replaced and written back to the same file.
The code looks as follows:
import fileinput def fixFile(fileName): # Open file for in-place replace for line in fileinput.FileInput(fileName, inplace=1): line = line.replace('findStr', 'replaceStr') print line # Put back line into file
The problem is that the written files have:
- One blank line inserted after every line.
- Ctrl-M character at the end of every line.
How do I prevent these extra appendages from getting inserted into the files?
Your newlines are coming from the print function
use:
and your line breaks will go away