file_util.py

Misc. file utility routines.

class openmdao.util.file_util.DirContext(dpath)[source]

Bases: object

Supports using the ‘with’ statement in place of try-finally to change to and return from a directory.

openmdao.util.file_util.build_directory(dct, force=False, topdir='.')[source]

Create a directory structure based on the contents of a nested dict. The directory is created in the specified top directory or in the current working directory if one isn’t specified. If a file being created already exists, a warning will be issued and the file will not be changed if force is False. If force is True, the file will be overwritten.

The structure of the dict is as follows: if the value at a key is a dict, then that key is used to create a directory. Otherwise, the key is used to create a file, and the value stored at that key is written to the file. All keys must be relative names or a RuntimeError will be raised.

Args:

dct : dict

Dictionary containing nested folder structure.

force : bool, optional

Set to True to overwrite existing files

topdir : string, optional

Specify a top directory.

openmdao.util.file_util.find_files(start, match=None, exclude=None, dirmatch=None, direxclude=None)[source]

Return filenames (using a generator).

Walks all subdirectories below each specified starting directory, subject to directory filtering.

Args:

start : str or list of str

Starting directory or list of directories.

match : str or predicate funct

Either a string containing a glob pattern to match or a predicate function that returns True on a match. This is used to match files only.

exclude : str or predicate funct

Either a string containing a glob pattern to exclude or a predicate function that returns True to exclude. This is used to exclude files only.

dirmatch : str or predicate funct

Either a string containing a glob pattern to match or a predicate function that returns True on a match. This is used to match directories only.

direxclude : str or predicate funct

Either a string containing a glob pattern to exclude or a predicate function that returns True to exclude. This is used to exclude directories only.

openmdao.util.file_util.find_up(name, path=None)[source]

Search upward from the starting path (or the current directory) until the given file or directory is found. The given name is assumed to be a basename, not a path. Returns the absolute path of the file or directory if found, or None otherwise.

Args:

name : str

Base name of the file or directory being searched for.

path : str, optional

Starting directory. If not supplied, current directory is used.