# Writing TH1's while opening multiple TFiles

**URL:** <https://root-forum.cern.ch/t/writing-th1s-while-opening-multiple-tfiles/41167>\
**Category:** Newbie\
**Created:** [August 26, 2020, 5:01pm UTC](https://root-forum.cern.ch/t/writing-th1s-while-opening-multiple-tfiles/41167 "2020-08-26T17:01:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Newbie](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@Newbie](https://root-forum.cern.ch/u/Newbie)\
**Post date:** [August 26, 2020, 5:01pm UTC](https://root-forum.cern.ch/t/writing-th1s-while-opening-multiple-tfiles/41167/1 "2020-08-26T17:01:21Z")

</div>

Dear, I have an issue here when trying to Write TH1’s in a rootfile:

> for i in range(0,len(rootfile\_list):  
> file1 = ROOT.TFile(rootfile\_list[i], “read”)  
> ihist = file1.Get(“hist”)  
> Hists\_list.append(ihist)
> 
> outfile = ROOT.TFile(‘MCplotsFudgeFactors.root’,‘create’)  
> for i in range(0,len(Hists\_list)):  
> Hists\_list[i].Write()  
> outfile.Close()

However, an error appears saying:

> Traceback (most recent call last):  
> File “PythonScript.py”, line 31, in   
> Hists\_list[i].Write()  
> AttributeError: ‘PyROOT\_NoneType’ object has no attribute ‘Write’

When I print the Hists\_list before the creation of the outfile, it looks fine, but after that, all the elements of the list appears as “None” type. Is there a reason why this is happening? How can I avoid it?

---

<div class="post-metadata">

**Author:** ![linev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/2bfe46/32.png) [@linev](https://root-forum.cern.ch/u/linev)\
**Post date:** [August 26, 2020, 6:10pm UTC](https://root-forum.cern.ch/t/writing-th1s-while-opening-multiple-tfiles/41167/2 "2020-08-26T18:10:28Z")

</div>

Hi,

You need to call `ihist.SetDirectory(0)` after it read from the file.

Regards,  
Sergey

---

<div class="post-metadata">

**Author:** ![Newbie](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@Newbie](https://root-forum.cern.ch/u/Newbie)\
**Post date:** [August 26, 2020, 7:59pm UTC](https://root-forum.cern.ch/t/writing-th1s-while-opening-multiple-tfiles/41167/3 "2020-08-26T19:59:32Z")

</div>

Thank you very much!!!
