Is __enter__ method missing in TFile of pyroot?

Hi, I’m a new user of pyroot. Today I followed the description of TFile.

It says we can use with... as... to close TFile object automatically. But when I try to do this:

with ROOT.TFile(trackeff_path, "read") as f1:
    h1 = f1.Get("heffratio")

I got

Traceback (most recent call last):
  File "/publicfs/ucas/user/xzh6313/xzh/RKst/test_pyroot/", line 7, in <module>
    with ROOT.TFile(trackeff_path, "read") as f1:
AttributeError: __enter__

Looks like the __enter__ is called, but there is no such method?

ROOT Version: 6.26/00

Welcome to the ROOT forum.
I think @etejedor can help you with pyroot.

Dear @Zhihao_Xu ,

That feature will be released with v6.28, it is now available only in ROOT master.

Thank you for your interest!


Dear @Zhihao_Xu ,
Thank you for taking interest in the feature. I believe you found it by reading the TFile reference website at ROOT: TFile Class Reference .

Notice that there is a drop menu on the top-left part of the page through which you can choose the ROOT version you are using. In your case it’s 6.26, so the corresponding TFile reference would be at ROOT: TFile Class Reference . You will see that in this page the context manager feature is not mentioned, so it’s not available in that release.


Thanks, Enric!

Thanks for your information, I didn’t see this drop menu before.

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