I am trying to replace some content in a file with the current working directory using python 3.3. I have:
def ReplaceInFile(filename, replaceRegEx, replaceWithRegEx):
''' Open a file and use a re.sub to replace content within it in place '''
with fileinput.input(filename, inplace=True) as f:
for line in f:
line = re.sub(replaceRegEx, replaceWithRegEx, line)
#sys.stdout.write (line)
print(line, end='')
and I am using it like so:
ReplaceInFile(r'Path\To\File.iss', r'(#define RootDir\s+)(.+)', r'\g<1>' + os.getcwd())
Unfortunately for me, my path is C:\Tkbt\Launch, so the substitution that I get is:
#define RootDir C: kbt\Launch
i.e. it’s interpreting \t as tab.
So it looks to me like I need to tell python to double escape everything from os.getcwd(). I thought maybe .decode('unicode_escape') might be the answer but it is not. Can anybody help me out?
I’m hoping there’s a solution that isn’t “find replace each '\' with '\\'“.
You’ll have to resort to
.replace('\\', '\\\\')I am afraid, that’s the only option you have to make this work.Using encoding to
unicode_escapethen decode again from ASCII would have been nice, if it worked:This does the right thing with paths:
but not with existing non-ASCII characters because
re.sub()does not process\uor\xescapes.Don’t use
re.escape()to escape special characters in a string, that escapes a little too much:note the
\:there.Only
.replace()results in a working replacement pattern, including non-ASCII characters: