0

I have created a hdf5 file using file = open() command. In this case, I can write and read the file. But it is giving me attribute error when I am trying file.keys(). The error is AttributeError: 'file' object has no attribute 'keys'.

Then I have created a new hdf5 file using file = h5py.File() command. In this case, I can read and use command file.keys() without any error. But I can not write in the file. The error is AttributeError: 'File' object has no attribute 'write'.

What are the reasons behind these error? Is there any difference between 'file' object and 'File' object?

sm10
  • 9
  • 1
  • 5

1 Answers1

1

open() returns an object of type file, that is the Python built in standard type to represent a file. This has quite a simple / low level interface and you would use it if you were reading a text file or parsing the content (be that text or binary) yourself. You can read the docs on the methods the file type has here - https://docs.python.org/2/library/stdtypes.html#bltin-file-objects

h5py.File() returns a different type of object that has additional functionality to handle the hdf5 format and provides it's own different API e.g. the keys() method you mention.

When opening a h5py.File() you must specify how you want to open it e.g. r+ for read/write mode. Someone with a better understanding of the h5py library may be able to give a better explanation for this but the reason you can not call write() on the h5py.File() object is because it does not have a write method as suggested by the error message.

Checkout the API docs for h5py, it provides different methods for writing different data to the file - http://docs.h5py.org/en/latest/high/dataset.html

Chris Wilding
  • 551
  • 3
  • 10