import os
import hou
import re
import shutil

cache_path = 'O:temp/dev/cache/test_SIM_v01/cool_cache.bgeo.sc'
directory = os.path.dirname(cache_path)
cache_file = os.path.basename(cache_path)
parts = cache_file.split('.')
cache_name = parts[0]
sim_folder = os.path.basename(directory)

if hou.hipFile.hasUnsavedChanges():
    hou.hipFile.save()
    
cache_dir = os.path.dirname(directory)
restore_dir = os.path.join(cache_dir, 'restore')

if not os.path.exists(restore_dir):
    os.makedirs(restore_dir)
    
current_scene = hou.hipFile.path()
restore_filename = '{0}_{1}_restore.hip'.format(sim_folder, cache_name)
restore_path = os.path.join(restore_dir, restore_filename)
shutil.copy2(current_scene, restore_path)

Gets the directory containing the cache file: 'O:/users/cooper.johnstone/setups/dev/cache/test_SIM_v01'

directory = os.path.dirname(cache_path)

Extracts just the filename from the path: 'cool_cache.bgeo.sc'

cache_file = os.path.basename(cache_path)

Splits the filename at periods, creating a list: ['cool_cache', 'bgeo', 'sc']

parts = cache_file.split('.')

Takes the first part of the split filename: 'cool_cache'

cache_name = parts[0]

Gets the name of the simulation folder: 'test_SIM_v01'

sim_folder = os.path.basename(directory)

Checks if the current Houdini scene has unsaved changes, and saves it if needed.

if hou.hipFile.hasUnsavedChanges():
    hou.hipFile.save()

Gets the cache dir

'O:/users/cooper.johnstone/setups/dev/cache'

cache_dir = os.path.dirname(directory)

Creates a path to a 'restore' subfolder: 'O:/users/cooper.johnstone/setups/dev/cache/restore'

restore_dir = os.path.join(cache_dir, 'restore')

Checks if the restore directory exists, and creates it if it doesn't.

if not os.path.exists(restore_dir):
    os.makedirs(restore_dir)