views:

1233

answers:

6

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:

  1. One blank line inserted after every line.
  2. Ctrl-M character at the end of every line.

How do I prevent these extra appendages from getting inserted into the files?

A: 

Try using file.write() instead of print.

You should not change the loop variable, and cannot write to the file you are reading from (text files are that way). Sorry, no in-place replace.

print will write to the standard output (sys.stdout) but can be redirected.

print also has an extended form, defined by the second portion of the syntax described above. This form is sometimes referred to as “print chevron.” In this form, the first expression after the >> must evaluate to a “file-like” object, specifically an object that has a write() method as described above. With this extended form, the subsequent expressions are printed to this file object. If the first expression evaluates to None, then sys.stdout is used as the file for output.

Suggested replace function, not in-place replace (outfile is a file-like object):

import fileinput
def fixFile(fileName, outfile):
    # Open file for in-place replace
    for line in fileinput.FileInput(fileName, inplace=1):
        outline = line.replace("findStr", "replaceStr")
        outfile.write(outline)  # Put line into outfile
gimel
+1  A: 

Use

print line,

or

file.write(line)

to fix extra newlines.

As of Ctrl-M - that is probably caused by input files in DOS encoding.

Eugene Morozov
Thanks. Your solution removes the blank lines but still has the Ctrl-M characters. Another person has already suggested an answer which solves both for me.
Ashwin
+4  A: 

your newlines are coming from the print function

use:

import sys

sys.stdout.write ('some stuff')

and your line breaks will go away

jottos
Ashwin
A: 

Use instead of

print line  # Put back line into file

this:

print line,  # Put back line into file
kwarnke
A: 

Hey gimel.

Don't you need to define outfile somewhere before you can write to it? I'm not asserting this. I am asking.

A: 

Change the first line in your for loop to:

line = line.rstrip().replace("findStr", "replaceStr")

kmote
Please explain the down-vote. This method addresses the newline problem (although I was not able to reproduce the Ctrl-M problem, so cannot speak to it).
kmote