From: BobAalsma on
I'm working on a set of scripts and I can't get a replace to work in
the script - please help.

The scripts show no errors, work properly apart from the replace, all
variables are filled as expected, the scripts works properly when the
commands are copied to the Python shell.

Text Main:
...
from LeadDevice_klant_nieuw_bewerken_bestanden import omkattenBoom,
omkattenNaam
....
#
# (3) omkatten bestandsnamen:
#
print 'Omkatten bestandsnamen'
omkattenNaam(AANGRIJPINGSPUNT, KLANTNAAM_OUT, KLANTNAAM_IN,
ZOEKSET1_OUT, ZOEKSET1_IN, ZOEKSET2_OUT, ZOEKSET2_IN)
#
.....


Text LeadDevice_klant_nieuw_bewerken_bestanden:
import os
import distutils.core

....

def omkattenNaam(AANGRIJPINGSPUNT, KLANTNAAM_OUT, KLANTNAAM_IN,
ZOEKSET1_OUT, ZOEKSET1_IN, ZOEKSET2_OUT, ZOEKSET2_IN):
#
# Strings opbouwen voordat aan de lussen gewerkt wordt:
#
beginpunt = AANGRIJPINGSPUNT + KLANTNAAM_IN + '/'
#
ZOEKSET1_OUT_LOWER = ZOEKSET1_OUT.lower()
ZOEKSET1_IN_LOWER = ZOEKSET1_IN.lower()
ZOEKSET2_OUT_LOWER = ZOEKSET2_OUT.lower()
ZOEKSET2_IN_LOWER = ZOEKSET2_IN.lower()
#
# Lussen:
#
for root, dirs, files in os.walk(beginpunt, topdown = False):
for bestandsnaam in dirs and files:
#
bestandsnaam_nieuw = bestandsnaam
bestandsnaam_nieuw.replace(KLANTNAAM_OUT,KLANTNAAM_IN)
From: Anthony Tolle on
On Aug 4, 9:10 am, BobAalsma <bob.aal...(a)aalsmacons.nl> wrote:
>                         #
>                         bestandsnaam_nieuw = bestandsnaam
>                         bestandsnaam_nieuw.replace(KLANTNAAM_OUT,KLANTNAAM_IN)

The replace method does not modify the string (strings are immutable).

You need to use the retun value of the method in an assignment, like
so:

bestandsnaam_nieuw = bestandsnaam.replace(KLANTNAAM_OUT,KLANTNAAM_IN)

This will not change the value of bestandsnaam
From: Peter Otten on
BobAalsma wrote:

Although [it] may not be obvious at first unless you're Dutch...

> bestandsnaam_nieuw = bestandsnaam
> bestandsnaam_nieuw.replace(KLANTNAAM_OUT,KLANTNAAM_IN)

str.replace() does not modify a string, it creates a new one.

This doesn't work:

>>> s = "that's all folks"
>>> s.replace("all", "nothing")
"that's nothing folks"
>>> s
"that's all folks"

But this does:

>>> old = "that's all folks"
>>> new = old.replace("all", "nothing")
>>> new
"that's nothing folks"

Peter
From: BobAalsma on
On Aug 4, 3:22 pm, Anthony Tolle <anthony.to...(a)gmail.com> wrote:
> On Aug 4, 9:10 am, BobAalsma <bob.aal...(a)aalsmacons.nl> wrote:
>
> >                         #
> >                         bestandsnaam_nieuw = bestandsnaam
> >                         bestandsnaam_nieuw.replace(KLANTNAAM_OUT,KLANTNAAM_IN)
>
> The replace method does not modify the string (strings are immutable).
>
> You need to use the retun value of the method in an assignment, like
> so:
>
> bestandsnaam_nieuw = bestandsnaam.replace(KLANTNAAM_OUT,KLANTNAAM_IN)
>
> This will not change the value of bestandsnaam

YESS!

Thanks, this is what I wanted to achieve but could not find

Regards,
Bob
From: Mike Kent on
On Aug 4, 9:10 am, BobAalsma <bob.aal...(a)aalsmacons.nl> wrote:
> I'm working on a set of scripts and I can't get a replace to work in
> the script - please help.

>                         bestandsnaam_nieuw.replace(KLANTNAAM_OUT,KLANTNAAM_IN)

I'm not sure what you are intending to do here, but string.replace
does not do its replacement in-place. It returns a copy of the
original string, with the replacement done in the copy. You are not
assigning the string returned by string.replace to anything,
therefore, it is immediately thrown away.

Secondly, and this is just a guess, but since you are doing the
string.replace inside of an os.walk loop, you appear to be trying to
do a filename change. I hope you realize that this will in no way
change the name of the file *on disk*; it will only change it in
memory.