changing names of items in a list
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: changing names of items in a list
royG wrote: 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[i] = 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: changing names of items in a list
On Wed, Mar 19, 2008 at 2:21 PM, royG [EMAIL PROTECTED] wrote: 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. [EMAIL PROTECTED] http://www.brunningonline.net/simon/blog/ GTalk: simon.brunning | MSN: small_values | Yahoo: smallvalues -- http://mail.python.org/mailman/listinfo/python-list