stadelmanma / netl-ap-map-flow

A fracture flow modeling package utilizing a modified local cubic law approach with OpenFoam and ParaView compatibility.
GNU General Public License v3.0
5 stars 4 forks source link

Make a function to handle the existance check as well as make directories #81

Open stadelmanma opened 7 years ago

stadelmanma commented 7 years ago

I have a very common pattern in my python code where I check if a file exists at a given path, raise an exception if it does and if not make all the parent directories. This will help DRY out my code and enforce a consistent logic for file creation throughout the code base.

I could call it prepare_output_path, possible code below, and it would go in the __core__.py file

def prepare_output_path(filename, overwrite=False, err_msg='{} file already exists'):
    r"""
    Checks if a file already exists and should not be overwritten, creating
    intermediate directories as needed if the file will be created.
    """
    if os.path.exists(filename) and not overwrite:
        raise FileExistsError(err_msg.format(filename))
    #
    os.makedirs(os.path.split(filename)[0], exist_ok=True)