[Tutor] Sorting a directory

Kent Johnson kent37 at tds.net
Tue May 24 17:00:27 CEST 2005


Jonas Melian wrote:
> Kent Johnson wrote:
>>files = glob.glob(os.path.join(dir_locales, "*_[A-Z][A-Z]"))
>>files.sort(key=lastTwoChars)	# Note: NOT key=lastTwoChars()
> 
> I get:
> ::
> files.sort(key=lastTwoChars)
> TypeError: sort() takes no keyword arguments
> ::
> 
> Could it be by the Python version? I have Python 2.3.5

Yes, the key= parameter is new in Python 2.4. For Python 2.3 you can define a comparison function, e.g.

def cmpLastTwoChars(a, b):
   return cmp(a[-2:], b[-2:])

files.sort(cmpLastTwoChars)

Kent



More information about the Tutor mailing list