# Error reading all requested bytes from file

**URL:** <https://root-forum.cern.ch/t/error-reading-all-requested-bytes-from-file/51301>\
**Category:** Newbie\
**Tags:** tree, root\
**Created:** [August 23, 2022, 10:20pm UTC](https://root-forum.cern.ch/t/error-reading-all-requested-bytes-from-file/51301 "2022-08-23T22:20:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![azinbeigi](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@azinbeigi](https://root-forum.cern.ch/u/azinbeigi)\
**Post date:** [August 23, 2022, 10:20pm UTC](https://root-forum.cern.ch/t/error-reading-all-requested-bytes-from-file/51301/1 "2022-08-23T22:20:44Z")

</div>

Hi everyone,

I have thousands of .root data files that I would like to combine using hadd. The problem is that some of them cannot be opened and I would like to come up with a script that loops through the files and remove the null ones and then do hadd, but my knowledge of C and C++ is limited. Anyone has any suggestions?

Thank you 🙂

---

<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:** [August 24, 2022, 7:17am UTC](https://root-forum.cern.ch/t/error-reading-all-requested-bytes-from-file/51301/2 "2022-08-24T07:17:17Z")

</div>

Hi @azinbeigi ,

and welcome to the ROOT forum! You can do this from C++ or Python equivalently, you want this:

```python
good_files = []
for filename in filenames:
    f = ROOT.TFile.Open(filename)
    if (f is not None and not f.IsZombie())
        good_files.append(filename)

```

(similarly for C++, instead of `None` you will have a `nullptr`).

See also our [manual](https://root.cern/manual/) and the [primer](https://root.cern/primer/).

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![azinbeigi](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@azinbeigi](https://root-forum.cern.ch/u/azinbeigi)\
**Post date:** [August 31, 2022, 2:15am UTC](https://root-forum.cern.ch/t/error-reading-all-requested-bytes-from-file/51301/3 "2022-08-31T02:15:02Z")

</div>

Hi Enrico,

Thank you for your response. So, I rand this simple python code:

import os  
import ROOT  
good\_files=[]  
path\_of\_the\_directory= ‘/home/shirin/TPS/Xoft/ResultsFromCluster/fixed\_complete/’  
print(“Files and directories in a specified path:”)  
for filename in os.listdir(path\_of\_the\_directory):  
f = os.path.join(path\_of\_the\_directory,filename)  
f = ROOT.TFile.Open(filename)  
if (f is not f.IsZombie()):  
good\_files.append(filename)

I get this error when I run that:

Files and directories in a specified path:  
Error in TFile::ReadBuffer: error reading all requested bytes from file 635635501.xRay.root, got 290 of 300  
Error in TFile::Init: 635635501.xRay.root failed to read the file type data.  
Traceback (most recent call last):  
File “getridfiles.py”, line 9, in   
if (f is not None and not f.IsZombie()):  
ReferenceError: attempt to access a null-pointer

---

<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:** [August 31, 2022, 7:19am UTC](https://root-forum.cern.ch/t/error-reading-all-requested-bytes-from-file/51301/4 "2022-08-31T07:19:03Z")

</div>

> [@azinbeigi](#):
>
> if (f is not f.IsZombie()):

this line should be `if f is not None and not f.IsZombie():`

Without that, you will try to use files even if they could not be opened correctly, as it is for 635635501.xRay.root.

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![azinbeigi](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@azinbeigi](https://root-forum.cern.ch/u/azinbeigi)\
**Post date:** [September 1, 2022, 7:10pm UTC](https://root-forum.cern.ch/t/error-reading-all-requested-bytes-from-file/51301/5 "2022-09-01T19:10:06Z")

</div>

Thank you, but even with that line, I get the same error:

Error in TFile::ReadBuffer: error reading all requested bytes from file 635635501.xRay.root, got 290 of 300  
Error in TFile::Init: 635635501.xRay.root failed to read the file type data.  
Traceback (most recent call last):  
File “getridfiles.py”, line 9, in   
if (f is not None and not f.IsZombie()):  
ReferenceError: attempt to access a null-pointer

---

<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:** [September 6, 2022, 6:07am UTC](https://root-forum.cern.ch/t/error-reading-all-requested-bytes-from-file/51301/6 "2022-09-06T06:07:59Z")

</div>

Uhm weird, 635635501.xRay.root is a malformed file and it looks like we don’t manage to detect that when opening it. Can you share 635635501.xRay.root with us?
