sciris.sc_fileio.thisdir

thisdir(file=None, path=None, *args, aspath=None, **kwargs)[source]

Tiny helper function to get the folder for a file, usually the current file. If not supplied, then use the current file.

Parameters
  • file (str) – the file to get the directory from; usually __file__

  • path (str/list) – additional path to append; passed to os.path.join()

  • args (list) – also passed to os.path.join()

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

  • kwargs (dict) – passed to Path()

Returns

the full path to the folder (or filename if additional arguments are given)

Return type

filepath (str)

Examples:

thisdir = sc.thisdir() # Get folder of calling file
thisdir = sc.thisdir('.') # Ditto (usually)
thisdir = sc.thisdir(__file__) # Ditto (usually)
file_in_same_dir = sc.thisdir(path='new_file.txt')
file_in_sub_dir = sc.thisdir('..', 'tests', 'mytests.py') # Merge parent folder with sufolders and a file
np_dir = sc.thisdir(np) # Get the folder that Numpy is loaded from (assuming "import numpy as np")
New in version 1.1.0: “as_path” argument renamed “aspath”
New in version 1.2.2: “path” argument
New in version 1.3.0: allow modules