1

Path looks like:

CLOUD_PATH = os.path.join(HOME, 'library', 'data') 
WORKDIR = os.getcwd()

Then in script I have a function:

def urlchanger(src, dst):                                                                

    xmlsdir = os.path.join(src, 'Plugins', '_xmls', '')                                  

    xmlfiles = [ f for f in os.listdir(xmlsdir) if re.match(r'^.*\.xml', f)]             

    for file in xmlfiles:                                                                

        with open(os.path.join(xmlsdir, file), 'r+') as f:                               
            indata = f.read()                                                                                                                 

            if ('dontchange.me' in indata):                                              
                outdata = re.sub(r'http://dontchange.me/', dst, indata)                  
                print 'Updating file %s:\n \n%s' % (os.path.join(xmlsdir, file), outdata)                                            

                with open((os.path.join(os.environ['TEMP'], file)), 'w') as n:           
                    n.write(outdata)                                                     

Which called with:

urlchanger(WORKDIR, CLOUD_PATH)  

Problem is, that re.sub performs substitution without trailing slash after data directory (result from out-file):

plugin assemblyUrl="C:\library\dataSomefile.dll"

Between data and Somefile - dataSomefile.

I tried add '' to CLOUD_PATH like:

CLOUD_PATH = os.path.join(HOME, 'library', 'data', '')

But got an error:

...
    raise error, v # invalid expression
sre_constants.error: bogus escape (end of line)

And same for few other attempts...

P.S. Script reads data from source file, looks for dontchange.me, replaces it with given URL, and will write new new file. Python 2.7.

setevoy
  • 4,374
  • 11
  • 50
  • 87
  • 2
    `if ('dontchange.me' in c for c in indata)` does not do what you want. It creates a generator and then tests the boolean value of that, which is _always true_. Did you mean `if any(the same generator expression)`? Also, it seems `indata` is a string, so `c` would be a character, so that expression does not make much sense in the first place... – tobias_k Apr 20 '15 at 13:50
  • I think `dst` is also sort-of treated as a regex, so if you are on windows and you have path separators like \ in there, they will be interpreted in the regex. You could pass a callable instead, just returning the `dst`. Please show a more complete code example, though! – tobias_k Apr 20 '15 at 13:54
  • @tobias_k yep, you are right :-) `in c for c in indata` left from previous coding (I tried `readlines` first, which return `list`). Thanks for note. – setevoy Apr 20 '15 at 13:55
  • @tobias_k added full function and it's call with arguments. – setevoy Apr 20 '15 at 13:58

1 Answers1

0

Found one solution here: Python how to replace backslash with re.sub()

So, result it:

if ('dontchange.me' in indata):                                        
    outdata = re.sub(r'http://dontchange.me/', dst + '\\\\', indata)   

But any other tips/advise appreciated.

Community
  • 1
  • 1
setevoy
  • 4,374
  • 11
  • 50
  • 87