write

FileSet.write(data, file_info, in_background=False, **write_args)[source]

Write content to a file by using the FileSet’s file handler.

If the filename extension is a compression format (such as zip, etc.) and FileSet.compress is set to true, the file will be compressed afterwards.

Notes

You need to specify a file handler for this fileset before you can use this method.

Parameters:
  • data – An object that can be stored by the used file handler class.

  • file_info – A string, path-alike object or a FileInfo object.

  • in_background – If true (default), this runs the writing process in a background thread so it does not pause the main process.

  • **write_args – Additional key word arguments for the write method of the used file handler object.

Returns:

None

Examples:

import matplotlib.pyplot as plt
from typhon.files import FileSet, Plotter

# Define a fileset consisting of multiple files:
plots = FileSet(
    path="/dir/{year}/{month}/{day}/{hour}{minute}{second}.png",
    handler=Plotter,
)

# Let's create a plot example
fig, ax = plt.subplots()
ax.plot([0, 1], [0, 1])
ax.set_title("Data from 2018-01-01")

## To save the plot as a file of the fileset, you have two options:
# Use this simple expression:
plots["2018-01-01"] = fig

# OR use write in combination with get_filename
filename = plots.get_filename("2018-01-01")
plots.write(fig, filename)

# Hint: If saving the plot takes a lot of time but you want to
# continue with the program in the meanwhile, you can use the
# *in_background* option. This saves the plot in a background
# thread.
plots.write(fig, filename, in_background=True)

# continue with other stuff immediately and do not wait until the
# plot is saved...
do_other_stuff(...)