polyfemos.util.fileutils — polyfemos 0.3.29 documentation

# polyfemos.util.fileutils

Functions for reading and writing files.

copyright

  2019, University of Oulu, Sodankyla Geophysical Observatory

license

  GNU Lesser General Public License v3.0 or later (https://spdx.org/licenses/LGPL-3.0-or-later.html)

Public Functions

`polyfemos.util.fileutils.` `append_to_file`(filename, text, header)

  If path defined in `filename` does not exist, new file is created and `header` is written in the file. If `text` is list, writes csv.

  Parameters

    - filename (str) –

    - text (str) –

    - header (str) –

`polyfemos.util.fileutils.` `check_dirpath`(func_)

`polyfemos.util.fileutils.` `check_filepath`(func_)

`polyfemos.util.fileutils.` `check_path`(dir_or_file)

  Returns a decorator that can be composed with functions that take file or directory path as their first argument.

  Decorated functions will return `None` if file or directory path is nonexistent. Otherwise the function will be called normally.

  Parameters

    dir_or_file (str) – “dir” or “file”, select if returned decorator check the existance of files or directories

  Return type

    func

  Returns

`polyfemos.util.fileutils.` `create_missing_folders`(filename)

  Creates full path to given `filename`.

  Parameters

    filename (str) – file or folder

`polyfemos.util.fileutils.` `get_stream`(path='', format_='MSEED')
  Parameters

    - path (str) – path to seismic data file

    - format_ (str, optional) – defaults to `MSEED`

  Return type

    `Stream` or None

  Returns

    If invalid miniseed file(path) is provided, empty stream is returned.

`polyfemos.util.fileutils.` `invalid_mseed`(path)

  Checks if miniseed file is valid and readable before reading it. At the moment only check is if filesize is between 128 and 2^31 in bytes.

  Parameters

    path (str) – path to file

  Return type

    str

  Returns

    returns message describing the problem with the file. If empty string is returned, no problems are present.

`polyfemos.util.fileutils.` `load_obj`(filename)

  A function to unpickle objects

  Parameters

    filename (str) – A file containing the pickled object

  Return type

  Returns

    Any object contained in the given file

`polyfemos.util.fileutils.` `load_yaml`(filename, **kwargs)
  Parameters

    filename (str) – path to YAML file

  Return type

    dict

  Returns

    read YAML file as dictionary

`polyfemos.util.fileutils.` `read_csv`(filename, delimiter=';', **kwargs)
  Parameters

    - filename (str) – filename of the csv to be read

    - delimiter (str, optional) – defaults to ‘;’, csv delimiter

  Return type

    list

  Returns

    list of lists containing the data from the given csv file

`polyfemos.util.fileutils.` `read_file`(filename, mode='r', **kwargs)
  Parameters

    - filename (str) – filename of the file to be read

    - mode (str, optional) – defaults to ‘r’ (read)

  Return type

    list

  Returns

    rows of the given file as list

`polyfemos.util.fileutils.` `render_template`(template_file, dict_)
  Parameters

    - template_file (str) – template file

    - dict_ (dict) – dictionary to be rendered to the template

`polyfemos.util.fileutils.` `rowsof`(filename, delims=' ', **kwargs)

  Yields the rows of the file as lists delimited by `delims`. Empty entries in lists are removed. Empty rows are removed.

  Parameters

    - filename (str) –

    - delims (iterable, optional) – defaults to ‘” “’ (a whitespace), For example, list or string containing all accepted delimiters.

  Returns

    A generator yielding rows of the file (`filename`) as lists.

`polyfemos.util.fileutils.` `save_obj`(filename, obj)

  A function to pickle objects

  Parameters

    - filename (str) – A file where the object will be saved

    - obj – Any object that can be pickled

`polyfemos.util.fileutils.` `write_csv`(filename, rows, mode='w', delimiter=';', singlerow=False)
  Parameters

    - filename (str) – filename of the csv created

    - rows (list) – list of list be written into csv

    - mode (str, optional) – defaults to ‘w’ (write)

    - delimiter (str, optional) – defaults to ‘;’, csv delimiter

    - singlerow (bool, optional) – Write single or multiple rows to csv. If `False`, `rows` should be list of lists.

`polyfemos.util.fileutils.` `write_file`(filename, str_, mode='w', cmf=True)
  Parameters

    - filename (str) – filename of the file created

    - str_ (str) – a text to written

    - mode (str, optional) – defaults to ‘w’ (write)

    - cmf (bool, optional) – defaults to `True`, If `True` missing folder in `filename` aer created before writing