sciris.sc_fileio.makefilepath

makefilepath(filename=None, folder=None, ext=None, default=None, split=False, aspath=None, abspath=True, makedirs=True, checkexists=None, sanitize=False, die=True, verbose=False)[source]

Utility for taking a filename and folder – or not – and generating a valid path from them. By default, this function will combine a filename and folder using os.path.join, create the folder(s) if needed with os.makedirs, and return the absolute path.

Parameters
  • filename (str or Path) – the filename, or full file path, to save to – in which case this utility does nothing

  • folder (str/Path/list) – the name of the folder to be prepended to the filename; if a list, fed to os.path.join()

  • ext (str) – the extension to ensure the file has

  • default (str or list) – a name or list of names to use if filename is None

  • split (bool) – whether to return the path and filename separately

  • aspath (bool) – whether to return a Path object

  • makedirs (bool) – whether or not to make the folders to save into if they don’t exist

  • checkexists (bool) – if False/True, raises an exception if the path does/doesn’t exist

  • sanitize (bool) – whether or not to remove special characters from the path; see sc.sanitizefilename() for details

  • verbose (bool) – how much detail to print

Returns

the validated path (or the folder and filename if split=True)

Return type

filepath (str or Path)

Simple example:

filepath = sc.makefilepath('myfile.obj') # Equivalent to os.path.abspath(os.path.expanduser('myfile.obj'))

Complex example:

filepath = makefilepath(filename=None, folder='./congee', ext='prj', default=[project.filename, project.name], split=True, abspath=True, makedirs=True)

Assuming project.filename is None and project.name is “recipe” and ./congee doesn’t exist, this will makes folder ./congee and returns e.g. (‘/home/myname/congee’, ‘recipe.prj’)

New in version 1.1.0: “aspath” argument