# Not able to use "with TFile("input\_file\_name.root", "read") as" block with root 6.24/06

**URL:** <https://root-forum.cern.ch/t/not-able-to-use-with-tfile-input-file-name-root-read-as-block-with-root-6-24-06/49781>\
**Category:** Newbie\
**Created:** [April 28, 2022, 9:44am UTC](https://root-forum.cern.ch/t/not-able-to-use-with-tfile-input-file-name-root-read-as-block-with-root-6-24-06/49781 "2022-04-28T09:44:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sarthchoudhary](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/sarthchoudhary/32/26105_2.png) [@sarthchoudhary](https://root-forum.cern.ch/u/sarthchoudhary)\
**Post date:** [April 28, 2022, 9:44am UTC](https://root-forum.cern.ch/t/not-able-to-use-with-tfile-input-file-name-root-read-as-block-with-root-6-24-06/49781/1 "2022-04-28T09:44:31Z")

</div>

I had installed root from conda and been using both root as well as pyroot but this particular method doesn’t work.

```auto
import ROOT
from ROOT import TFile
with TFile("new_out_file24.root", "read") as infile:
    hin = infile.Get("H1_Rprompt_302_to_303_qPE_pass_rCut_py_+model") 

print(hin.GetName())

```

I can read the file if I do it this way:

```auto
import ROOT
from ROOT import TFile
infile = TFile("new_out_file24.root", "read")
hin = infile.Get("H1_Rprompt_302_to_303_qPE_pass_rCut_py_+model") 
print(hin.GetName())
infile.Close()

```

_ROOT Version:_ 6.24/06  
_Platform:_ Ubuntu 20.04  
_Compiler:_ Not Provided

* * *

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [April 28, 2022, 10:57am UTC](https://root-forum.cern.ch/t/not-able-to-use-with-tfile-input-file-name-root-read-as-block-with-root-6-24-06/49781/2 "2022-04-28T10:57:02Z")

</div>

Hello,

The context manager functionality for `TFile` that you are trying to use was added recently to master, there’s a tutorial for it:

> <https://github.com/root-project/root/blob/master/tutorials/pyroot/pyroot005_tfile_context_manager.py>

It will be released with ROOT 6.28, so unfortunately you can’t use it yet with 6.24.

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [April 28, 2022, 11:56am UTC](https://root-forum.cern.ch/t/not-able-to-use-with-tfile-input-file-name-root-read-as-block-with-root-6-24-06/49781/3 "2022-04-28T11:56:42Z")

</div>

Note however that

```python
import ROOT
from ROOT import TFile
with TFile("new_out_file24.root", "read") as infile:
    hin = infile.Get("H1_Rprompt_302_to_303_qPE_pass_rCut_py_+model") 

print(hin.GetName())

```

will not work: because of ROOT lifetime management, the `hin` histogram is deleted when the file is closed. Maybe adding a `hin.SetDirectory(0)` would help in that case.

---

<div class="post-metadata">

**Author:** ![sarthchoudhary](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/sarthchoudhary/32/26105_2.png) [@sarthchoudhary](https://root-forum.cern.ch/u/sarthchoudhary)\
**Post date:** [April 28, 2022, 12:10pm UTC](https://root-forum.cern.ch/t/not-able-to-use-with-tfile-input-file-name-root-read-as-block-with-root-6-24-06/49781/4 "2022-04-28T12:10:48Z")

</div>

> [@etejedor](#):
>
> will not work: because of ROOT lifetime management, the `hin` histogram is deleted when the file is closed. Maybe adding a `hin.SetDirectory(0)` would help in that case.

Ah yes, I didn’t had that in mind. Thanks for pointing it out.
