# Filenode Module

A file interface to nodes for PyTables databases.

The FileNode module provides a file interface for using inside of PyTables database files. Use the new_node() function to create a brand new file node which can be read and written as any ordinary Python file. Use the open_node() function to open an existing (i.e. created with new_node()) node for read-only or read-write access. Read acces is always available. Write access (enabled on new files and files opened with mode ‘a+’) only allows appending data to a file node.

Currently only binary I/O is supported.

See filenode - simulating a filesystem with PyTables for instructions on use.

Changed in version 3.0: In version 3.0 the module as been completely rewritten to be fully compliant with the interfaces defined in the `io` module.

## Module constants

`tables.nodes.filenode.``NodeType` = 'file'

  Value for NODE_TYPE node system attribute.

`tables.nodes.filenode.``NodeTypeVersions` = [1, 2]

  Supported values for NODE_TYPE_VERSION node system attribute.

## Module functions

`tables.nodes.filenode.``new_node`(h5file, **kwargs)

  Creates a new file node object in the specified PyTables file object.

  Additional named arguments where and name must be passed to specify where the file node is to be created. Other named arguments such as title and filters may also be passed.

  The special named argument expectedsize, indicating an estimate of the file size in bytes, may also be passed. It returns the file node object.

`tables.nodes.filenode.``open_node`(node, mode='r')

  Opens an existing file node.

  Returns a file node object from the existing specified PyTables node. If mode is not specified or it is ‘r’, the file can only be read, and the pointer is positioned at the beginning of the file. If mode is ‘a+’, the file can be read and appended, and the pointer is positioned at the end of the file.

`tables.nodes.filenode.``read_from_filenode`(h5file, filename, where, name=None, overwrite=False, create_target=False)

  Read a filenode from a PyTables file and write its contents to a file.

  New in version 3.2.

  Parameters: | h5file : The PyTables file to be read from; can be either a string giving the file’s location or a `File` object. filename : Path of the file where the contents of the filenode shall be written to. If filename points to a directory or ends with `/` (`\` on Windows), the filename will be set to the _filename (if present; otherwise the name) attribute of the read filenode. where, name : Location of the filenode where the data shall be read from. If no node name can be found at where, the first node at where whose _filename attribute matches name will be read. overwrite : Whether or not a possibly existing file of the specified filename shall be overwritten. create_target : Whether or not the folder hierarchy needed to accomodate the given target `filename` will be created.

`tables.nodes.filenode.``save_to_filenode`(h5file, filename, where, name=None, overwrite=False, title='', filters=None)

  Save a file’s contents to a filenode inside a PyTables file.

  New in version 3.2.

  Parameters: | h5file : The PyTables file to be written to; can be either a string giving the file’s location or a `File` object. If a file with name h5file already exists, it will be opened in mode `a`. filename : Path of the file which shall be stored within the PyTables file. where, name : Location of the filenode where the data shall be stored. If name is not given, and where is either a `Group` object or a string ending on `/`, the leaf name will be set to the file name of filename. The name will be modified to adhere to Python’s natural naming convention; the original filename will be preserved in the filenode’s _filename attribute. overwrite : Whether or not a possibly existing filenode of the specified name shall be overwritten. title : A description for this node (it sets the `TITLE` HDF5 attribute on disk). filters : An instance of the `Filters` class that provides information about the desired I/O filters to be applied during the life of this object.

## The RawPyTablesIO base class

class `tables.nodes.filenode.``RawPyTablesIO`(node, mode=None)

  Base class for raw binary I/O on HDF5 files using PyTables.

### RawPyTablesIO attributes

`RawPyTablesIO.``mode`

  File mode.

### RawPyTablesIO methods

`RawPyTablesIO.``tell`()

  Return current stream position.

`RawPyTablesIO.``seek`(pos, whence=0)

  Change stream position.

  Change the stream position to byte offset offset. offset is interpreted relative to the position indicated by whence. Values for whence are:

  - 0 – start of stream (the default); offset should be zero or positive
  - 1 – current stream position; offset may be negative
  - 2 – end of stream; offset is usually negative

  Return the new absolute position.

`RawPyTablesIO.``seekable`()

  Return whether object supports random access.

  If False, seek(), tell() and truncate() will raise IOError. This method may need to do a test seek().

`RawPyTablesIO.``fileno`()

  Returns underlying file descriptor if one exists.

  An IOError is raised if the IO object does not use a file descriptor.

`RawPyTablesIO.``close`()

  Flush and close the IO object.

  This method has no effect if the file is already closed.

`RawPyTablesIO.``flush`()

  Flush write buffers, if applicable.

  This is not implemented for read-only and non-blocking streams.

`RawPyTablesIO.``truncate`(pos=None)

  Truncate file to size bytes.

  Size defaults to the current IO position as reported by tell(). Return the new size.

  Currently, this method only makes sense to grow the file node, since data can not be rewritten nor deleted.

`RawPyTablesIO.``readable`()

  Return whether object was opened for reading.

  If False, read() will raise IOError.

`RawPyTablesIO.``writable`()

  Return whether object was opened for writing.

  If False, write() and truncate() will raise IOError.

`RawPyTablesIO.``readinto`(b)

  Read up to len(b) bytes into b.

  Returns number of bytes read (0 for EOF), or None if the object is set not to block as has no data to read.

`RawPyTablesIO.``readline`(limit=-1)

  Read and return a line from the stream.

  If limit is specified, at most limit bytes will be read.

  The line terminator is always `\n` for binary files; for text files, the newlines argument to open can be used to select the line terminator(s) recognized.

`RawPyTablesIO.``write`(b)

  Write the given buffer to the IO stream.

  Returns the number of bytes written, which may be less than len(b).

## The ROFileNode class

class `tables.nodes.filenode.``ROFileNode`(node)

  Creates a new read-only file node.

  Creates a new read-only file node associated with the specified PyTables node, providing a standard Python file interface to it. The node has to have been created on a previous occasion using the new_node() function.

  The node used as storage is also made available via the read-only attribute node. Please do not tamper with this object if it’s avoidable, since you may break the operation of the file node object.

  The constructor is not intended to be used directly. Use the open_node() function in read-only mode (‘r’) instead.

  Version 1: | implements the file storage as a UInt8 uni-dimensional EArray.
  Version 2: | uses an UInt8 N vector EArray.

  Changed in version 3.0: The offset attribute is no more available, please use seek/tell methods instead.

  Changed in version 3.0: The line_separator property is no more available. The only line separator used for binary I/O is `\n`.

### ROFileNode attributes

`ROFileNode.``attrs`

  A property pointing to the attribute set of the file node.

### ROFileNode methods

`ROFileNode.``flush`()

  Flush write buffers, if applicable.

  This is not implemented for read-only and non-blocking streams.

`ROFileNode.``read`()

`ROFileNode.``readline`(limit=-1)

  Read and return a line from the stream.

  If limit is specified, at most limit bytes will be read.

  The line terminator is always `\n` for binary files; for text files, the newlines argument to open can be used to select the line terminator(s) recognized.

`ROFileNode.``readlines`()

  Return a list of lines from the stream.

  hint can be specified to control the number of lines read: no more lines will be read if the total size (in bytes/characters) of all lines so far exceeds hint.

`ROFileNode.``close`()

  Flush and close the IO object.

  This method has no effect if the file is already closed.

`ROFileNode.``seek`(pos, whence=0)

  Change stream position.

  Change the stream position to byte offset offset. offset is interpreted relative to the position indicated by whence. Values for whence are:

  - 0 – start of stream (the default); offset should be zero or positive
  - 1 – current stream position; offset may be negative
  - 2 – end of stream; offset is usually negative

  Return the new absolute position.

`ROFileNode.``tell`()

  Return current stream position.

`ROFileNode.``readable`()

  Return whether object was opened for reading.

  If False, read() will raise IOError.

`ROFileNode.``writable`()

  Return whether object was opened for writing.

  If False, write() and truncate() will raise IOError.

`ROFileNode.``seekable`()

  Return whether object supports random access.

  If False, seek(), tell() and truncate() will raise IOError. This method may need to do a test seek().

`ROFileNode.``fileno`()

  Returns underlying file descriptor if one exists.

  An IOError is raised if the IO object does not use a file descriptor.

## The RAFileNode class

class `tables.nodes.filenode.``RAFileNode`(node, h5file, **kwargs)

  Creates a new read-write file node.

  The first syntax opens the specified PyTables node, while the second one creates a new node in the specified PyTables file. In the second case, additional named arguments ‘where’ and ‘name’ must be passed to specify where the file node is to be created. Other named arguments such as ‘title’ and ‘filters’ may also be passed. The special named argument ‘expectedsize’, indicating an estimate of the file size in bytes, may also be passed.

  Write access means reading as well as appending data is allowed.

  The node used as storage is also made available via the read-only attribute node. Please do not tamper with this object if it’s avoidable, since you may break the operation of the file node object.

  The constructor is not intended to be used directly. Use the new_node() or open_node() functions instead.

  Version 1: | implements the file storage as a UInt8 uni-dimensional EArray.
  Version 2: | uses an UInt8 N vector EArray.

  Changed in version 3.0: The offset attribute is no more available, please use seek/tell methods instead.

  Changed in version 3.0: The line_separator property is no more available. The only line separator used for binary I/O is `\n`.

### RAFileNode attributes

`RAFileNode.``attrs`

  A property pointing to the attribute set of the file node.

### RAFileNode methods

`RAFileNode.``flush`()

  Flush write buffers, if applicable.

  This is not implemented for read-only and non-blocking streams.

`RAFileNode.``read`()

`RAFileNode.``readline`(limit=-1)

  Read and return a line from the stream.

  If limit is specified, at most limit bytes will be read.

  The line terminator is always `\n` for binary files; for text files, the newlines argument to open can be used to select the line terminator(s) recognized.

`RAFileNode.``readlines`()

  Return a list of lines from the stream.

  hint can be specified to control the number of lines read: no more lines will be read if the total size (in bytes/characters) of all lines so far exceeds hint.

`RAFileNode.``truncate`(pos=None)

  Truncate file to size bytes.

  Size defaults to the current IO position as reported by tell(). Return the new size.

  Currently, this method only makes sense to grow the file node, since data can not be rewritten nor deleted.

`RAFileNode.``write`(b)

  Write the given buffer to the IO stream.

  Returns the number of bytes written, which may be less than len(b).

`RAFileNode.``writelines`()

`RAFileNode.``close`()

  Flush and close the IO object.

  This method has no effect if the file is already closed.

`RAFileNode.``seek`(pos, whence=0)

  Change stream position.

  Change the stream position to byte offset offset. offset is interpreted relative to the position indicated by whence. Values for whence are:

  - 0 – start of stream (the default); offset should be zero or positive
  - 1 – current stream position; offset may be negative
  - 2 – end of stream; offset is usually negative

  Return the new absolute position.

`RAFileNode.``tell`()

  Return current stream position.

`RAFileNode.``readable`()

  Return whether object was opened for reading.

  If False, read() will raise IOError.

`RAFileNode.``writable`()

  Return whether object was opened for writing.

  If False, write() and truncate() will raise IOError.

`RAFileNode.``seekable`()

  Return whether object supports random access.

  If False, seek(), tell() and truncate() will raise IOError. This method may need to do a test seek().

`RAFileNode.``fileno`()

  Returns underlying file descriptor if one exists.

  An IOError is raised if the IO object does not use a file descriptor.