Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9125259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:45:47+00:00 2026-06-17T06:45:47+00:00

I am trying to replace some content in a file with the current working

  • 0

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 '\\'“.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T06:45:48+00:00Added an answer on June 17, 2026 at 6:45 am

    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_escape then decode again from ASCII would have been nice, if it worked:

      replacepattern = r'\g<1>' + os.getcwd().encode('unicode_escape').decode('ascii')
      

      This does the right thing with paths:

      >>> print(re.sub(r'(#define RootDir\s+)(.+)', r'\g<1>' + r'C:\Path\to\File.iss'.encode('unicode_escape').decode('ascii'), '#define Root
      #define RootDir C:\Path\to\File.iss
      

      but not with existing non-ASCII characters because re.sub() does not process \u or \x escapes.

    • Don’t use re.escape() to escape special characters in a string, that escapes a little too much:

      >>> print(re.sub(r'(#define RootDir\s+)(.+)', r'\g<1>' + re.escape(r'C:\Path\To\File.iss'), '#define RootDir foo/bar/baz'))
      #define RootDir C\:\Path\To\File\.iss
      

      note the \: there.

    Only .replace() results in a working replacement pattern, including non-ASCII characters:

    >>> print(re.sub(r'(#define RootDir\s+)(.+)', r'\g<1>' + 'C:\\Path\\To\\File-with-non-
    ASCII-\xef.iss'.replace('\\', '\\\\'), '#define Root
    #define RootDir C:\Path\To\File-with-non-ASCII-ï.iss
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get file contents, replace some parts of it using regular expressions
I am trying to get up to speed with Python, trying to replace some
I need to replace several URLs in a text file with some content dependent
I'm trying to find some certain blocks in my data file and replace something
I'm trying to replace text in a multi-line file using command-line perl. I'm using
I'm currently working on some search and replace operation that I'm trying to automate
I have a text file and I'm trying to replace a specific character (.)
I am trying to upload file using dropbox api but following Code shows some
I am currently trying to replace some portions of my code with std::shared_ptr and
I am trying to replace some mathematical operators with variables, so that I can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.