newbie problem with str.replace

B

BobAalsma

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)
 
P

Peter Otten

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:
"that's all folks"

But this does:
"that's nothing folks"

Peter
 
A

Anthony Tolle

                        #
                        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
 
B

BobAalsma

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
 
M

Mike Kent

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top