changing names of items in a list

R

royG

hi
i am trying to rename extension of files in a directory..as an initial
step i made a method in

class ConvertFiles:
def __init__(self,infldr,outfldr):
self.infldr=infldr
self.outfldr=outfldr
self.origlist=os.listdir(infldr)
.....
def renamefiles(self,extn):
for x in self.origlist:
x=x+"."+extn

....

later when i print self.origlist i find that the elements of list are
unchanged..even tho a print x inside the renamefiles() shows that
extn is appended to x ..
why does this happen?
RG
 
D

Diez B. Roggisch

royG said:
hi
i am trying to rename extension of files in a directory..as an initial
step i made a method in

class ConvertFiles:
def __init__(self,infldr,outfldr):
self.infldr=infldr
self.outfldr=outfldr
self.origlist=os.listdir(infldr)
....
def renamefiles(self,extn):
for x in self.origlist:
x=x+"."+extn

...

later when i print self.origlist i find that the elements of list are
unchanged..even tho a print x inside the renamefiles() shows that
extn is appended to x ..
why does this happen?

Because a piece of code like this

x = some_list[index]
x = something_else()

doesn't change the object itself, just rebinds x to a new object. That the
old object stored in x happened to be referenced in a list as well - who
cares?

So either your members of origlist become mutables - then you have to alter
them, and then they will be reachable from the list. Like this:

class Foo(object):
def __init__(self):
pass

l = [Foo() for _ in xrang(10)]

f = l[0]
f.bar = "baz"

print l[0].bar

Or you rebind the list itself, or it's index under work - like this:

for i, item in enumerate(l):
l = do_something_with_item(item)

or

new_l = []
for item in l:
new_l.append(do_something(item))
l = new_l

There are innumerable variations of this scheme.

Diez
 
S

Simon Brunning

hi
i am trying to rename extension of files in a directory..as an initial
step i made a method in

class ConvertFiles:
def __init__(self,infldr,outfldr):
self.infldr=infldr
self.outfldr=outfldr
self.origlist=os.listdir(infldr)
....
def renamefiles(self,extn):
for x in self.origlist:
x=x+"."+extn

...

later when i print self.origlist i find that the elements of list are
unchanged..even tho a print x inside the renamefiles() shows that
extn is appended to x ..
why does this happen?

Your 'x=' line is building a brand new string, and rebinding the name
'x' to it. It's not doing anything to the original list. See
<http://effbot.org/zone/python-objects.htm>.

I'd rewrite that as (untested):

def renamefiles(self, extn):
self.origlist = list((x + "." + extn) for x in self.origlist)

or

def renamefiles(self, extn):
self.origlist = list(("%s.%s" % (z, extn)) for x in self.origlist)

Better still, take a look at the os.path module...

--
Cheers,
Simon B.
(e-mail address removed)
http://www.brunningonline.net/simon/blog/
GTalk: simon.brunning | MSN: small_values | Yahoo: smallvalues
 

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,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top