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)[source]¶ If path defined in
filename
does not exist, new file is created andheader
is written in the file. Iftext
is list, writes csv.
-
polyfemos.util.fileutils.
check_dirpath
(func_)¶
-
polyfemos.util.fileutils.
check_filepath
(func_)¶
-
polyfemos.util.fileutils.
check_path
(dir_or_file)[source]¶ 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)[source]¶ Creates full path to given
filename
.- Parameters
filename (str) – file or folder
-
polyfemos.util.fileutils.
invalid_mseed
(path)[source]¶ 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.
-
polyfemos.util.fileutils.
load_obj
(filename)[source]¶ 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.
rowsof
(filename, delims=' ', **kwargs)[source]¶ 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)[source]¶ 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)[source]¶ - 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.