| 1 |
.. toctree:: |
| 2 |
:maxdepth: 1 |
| 3 |
|
| 4 |
File handling |
| 5 |
============= |
| 6 |
|
| 7 |
The *File* entity encapsulates all information of a dataset. *File* |
| 8 |
entities keep some general information such as creation time and date, |
| 9 |
format version etc. It is the entry-point to a *NIX* file. |
| 10 |
|
| 11 |
File modes |
| 12 |
---------- |
| 13 |
|
| 14 |
A *NIX* file can be opened in one of three modes: |
| 15 |
|
| 16 |
1. ``nixio.FileMode.Overwrite`` Used for creating a new file or overwriting any content |
| 17 |
in an existing one! |
| 18 |
2. ``nixio.FileMode.ReadWrite`` Opens a file if it exists or otherwise creates a new |
| 19 |
one. Does **not** overwrite existing content. |
| 20 |
3. ``nixio.FileMode.ReadOnly`` Opens an existing file. The content can not be changed. |
| 21 |
Will raise an error when the provided path is invalid (e.g. the file |
| 22 |
does not exist). |
| 23 |
|
| 24 |
Creating a new file |
| 25 |
-------------------- |
| 26 |
|
| 27 |
.. literalinclude:: examples/fileCreate.py |
| 28 |
:language: python |
| 29 |
:lines: 3-13 |
| 30 |
:emphasize-lines: 7 |
| 31 |
:caption: Creating a new nix file with ``nixio.FileMode.Overwite`` or ``nixio.FileMode.ReadWrite`` (:download:`example code <examples/fileCreate.py>` |
| 32 |
|
| 33 |
As mentioned above, using ``nixio.FileMode.Overwrite`` will destroy any existing content of the file. **By default**, if the ``FileMode`` flag is not specified, a file is opened in the ``FileMode.ReadWrite`` mode. |
| 34 |
|
| 35 |
In this tutorial files will always have the extension ``*.nix``. This is not specified in the library and you are free to use any file extension. |
| 36 |
|
| 37 |
Opening existing files |
| 38 |
----------------------- |
| 39 |
|
| 40 |
Use ``nixio.FileMode.ReadWrite`` or ``nixio.FileMode.ReadOnly`` to open |
| 41 |
an existing file to work with the data. If you want to overwrite *any* |
| 42 |
existing content in order to replace the file use |
| 43 |
``nixio.FileMode.Overwite``. |
| 44 |
|
| 45 |
In ``ReadOnly`` mode, you cannot change the file content: |
| 46 |
|
| 47 |
.. literalinclude:: examples/fileCreate.py |
| 48 |
:language: python |
| 49 |
:lines: 14-23 |
| 50 |
|
| 51 |
In order to change the file content the file needs to be opened in ``ReadWrite`` mode: |
| 52 |
|
| 53 |
.. literalinclude:: examples/fileCreate.py |
| 54 |
:language: python |
| 55 |
:lines: 25-28 |
| 56 |
|
| 57 |
Trying to open an non-existing file in ``ReadOnly`` mode will lead to an |
| 58 |
error! |
| 59 |
|
| 60 |
.. literalinclude:: examples/fileCreate.py |
| 61 |
:language: python |
| 62 |
:lines: 31-38 |
| 63 |
|
| 64 |
Enabling compression |
| 65 |
-------------------- |
| 66 |
|
| 67 |
By default, data stored inside a *NIX* file will not be compressed. You |
| 68 |
can switch on compression during file opening. |
| 69 |
|
| 70 |
.. literalinclude:: examples/fileCreate.py |
| 71 |
:language: python |
| 72 |
:lines: 39-40 |
| 73 |
|
| 74 |
Compression is handled transparently by the hdf5-library, no further |
| 75 |
user interaction is required. Compression reduces reading and writing |
| 76 |
performance. You can choose to switch off compression on individual |
| 77 |
*DataArray*\ s by passing the ``nixio.Compression.No`` flag at |
| 78 |
the time of creation. |