AttributeError: 'str' object has no attribute 'cd'

Hi all,

it the loop bellow problem occurs. I wanna save some graphs to mutual file.root but in different directories. I mean to do it like this

scan_number = 1
f1= TFile(“file.root”,“UPDATE”)
directory = (“Scan%d”) %scan_number
if directory.cd()==True:
** directory.cd()**
** g.Write()**
else:
** f1.mkdir(directory)**
** directory.cd()**
** g.Write()**
f1.Close()

But I`m getting the error
AttributeError: ‘str’ object has no attribute 'cd’

I also tried
directory = “Scan”+ str(scan_number)
but same problem occured

How can I make convert directory name so I can use cd() command on it?

Thanks in advance,
V.

try

directory = f1.GetDirectory(  (“Scan%d”) %scan_number )

Thank you pcanal,
Your suggestion helped me a lot

This is how I solved it

f1= TFile("analyse.root","RECREATE")
inname = "Scan%d" %scan
directory = f1.GetDirectory(inname)
if directory==None:
    f1.mkdir(inname)
    dirget= f1.GetDirectory(inname)
    dirget.cd()
    g.Write()
else:
    directory.cd()
    g.Write()
f1.Close()

All the best,
V.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.