Iterate over a directory using python

Hello,

I was doing:

def constructDirContents(file): dirList = [] nextkey = TIter(file.GetListOfKeys()) key = TKey(nextkey.Next()) while key.GetTitle(): if key.IsFolder() != 1: dirItem = [key.ReadObj().ClassName(), key.GetName(), key.GetTitle()] dirList.append(dirItem) key = TKey(nextkey.Next()) return dirList

in order to construct a listing of all directory entries. Now I get an error that TKey() has no copy/constructor. Reading the changelogs and CVS I figured out that a copy-constructor was added to TKey to prevent the compiler from generating one, and this new copy-constructor is protected. Since there is no object casting in python when i try to do:

key = nextkey.Next()
key.GetTitle() 

I get an error of: ‘NoneType’ object has no attribute ‘GetTitle’.

Can you tell me another way to iterate over all of the keys in a given directory?

Hi,

there is something that I don’t understand: why do you use the TIter mechanism? This is the normal python way:[code]for key in file.GetListOfKeys():

… do stuff with key …[/code]

and if that doesn’t work for you, it’s a bug. If so, please sent me the file that you’re using, as well as the version of ROOT and platform that you’re running on, so that it can be fixed. Thanks.

Best regards,
Wim

Thank you, that works great, and makes a lot more sense from a python perspective.

Because I just learned python the other day and ported my C++ code to python =)[/quote]