earthkit.data.targets.file

Attributes

Classes

FileTarget

File target.

Module Contents

class earthkit.data.targets.file.FileTarget(file=None, *, append=False, **kwargs)

Bases: earthkit.data.targets.SimpleTarget

File target.

Parameters:
  • file (str, file-like, None) – The file path or file-like object to write to. When None, tries to guess the file name from the data if it is passed as a kwarg. When the file name cannot be constructed, a ValueError is raised. When file is a path, a file object is automatically created and closed when the target is closed. When file is a file object, its ownership is not transferred to the target. As a consequence, the file object is not closed when the target is closed, even if close is called explicitly.

  • append (bool) – If True, the file is opened in append mode. Only used if file is a path.

  • **kwargs – Additional keyword arguments passed to the parent class.

Raises:

ValueError

append = False
close()

Close the file if FileTarget was created with a file path.

If FileTarget was created with a file object this call has no effect. The target will not be able to write anymore.

Raises:

ValueError

property closed

True if the target is closed, False otherwise.

ext = None
filename = None
fileobj = None
flush()

Flush the file.

Raises:

ValueError

write(data=None, **kwargs)

Write data to the target using the given encoder.

Parameters:
  • data (obj, None) – The data object to write. If None, the encoder will use all the other arguments to generate the data to write.

  • encoder (str, Encoder, None) – The encoder to use to encode the data. When a string is passed, the encoder is looked up in the available encoders. When None, the encoder the Target was created with will be used if available. Otherwise, the encoder will be determined from the data to write (if possible) or from the Target properties.

  • template (obj, None) – The template to use to encode the data. When None, the template the Target was created with will be used if available.

  • metadata (dict, None) – Metadata to pass to the encoder.

  • **kwargs (dict) – Other keyword arguments passed to the encoder.

Raises:

ValueError

earthkit.data.targets.file.target