scistag.filestag.file_path.FilePath

class FilePath[source]

Bases: object

Helper class for handling file paths - such as detecting the path or the extension of a file.

Methods

absolute

Returns the absolute path for given relative path (relative to the current getcwd() path.

absolute_comb

Returns the absolute, normalized path of a relative path object combined with an absolute path object.

basename

Returns the path's base name (e.g the filename)

dirname

Returns the directory name of a file

exists

Returns if given path exists

make_dirs

Creates the defined directory and all directories in between

norm_path

Normalizes a path, e.g.

script_filename

Returns the file name of the calling method

script_path

Returns the file name of the calling method

split_ext

Returns the extension and file component of given file path

split_path_components

Returns the single path components as a list

Attributes

SEP

The OS specific path separator

__dict__

__doc__

__module__

__weakref__

list of weak references to the object (if defined)

classmethod absolute(path)[source]

Returns the absolute path for given relative path (relative to the current getcwd() path.

As of now just a wrapper of os.path.abspath().

Parameters

path (str) – The relative path e.g. “./../data”

Returns

The absolute path, e.g. “/home/user/scripts/data”

classmethod absolute_comb(rel_path, absolute_path=None)[source]

Returns the absolute, normalized path of a relative path object combined with an absolute path object.

What makes this function pretty handy is that if no path is given the calling script’s path or Jupyter notebook’s path will be used so relative includes can be easily located.

Parameters
  • rel_path (str) – The (relative) path of which we want to determine the absolute path of

  • absolute_path (str | None) – The path at which we orient - has to be absolute. If no path is passed the calling function or the location of the executing Jupyter notebook will be used.

Returns

The absolute path

static basename(path)[source]

Returns the path’s base name (e.g the filename)

As of now just a wrapper of os.path.basename().

Parameters

path (str) – The path

Return type

str

Returns

The element within the path, e.g. the file name

static dirname(filename, slash=True)[source]

Returns the directory name of a file

As of now just a wrapper of os.path.dirname().

Parameters
  • filename (str) – The file’s name

  • slash (bool) – If passed it will normalize the path to Unix style using slashes only which is supported by Windows and Linux in most cases. True by default.

Return type

str

Returns

The directory the file is within

static exists(path)[source]

Returns if given path exists

As of now just a wrapper of os.path.exists().

Parameters

path (str) – The path name

Return type

bool

Returns

True if it exists

classmethod make_dirs(path, exist_ok=False)[source]

Creates the defined directory and all directories in between

Parameters
  • path (str) – The path of the directory to create

  • exist_ok (bool) – Defines if the operation shall fail if the directory already exists.

Return type

bool

Returns

True on success

static norm_path(path, slash=True)[source]

Normalizes a path, e.g. integrates relative path definitions such as .. and . into the path.

As of now just a wrapper of os.path.normpath().

Parameters
  • path (str) – The path, e.g. /home/user/data/../documents

  • slash (bool) – If passed it will normalize the path to Unix style using slashes only which is supported by Windows and Linux in most cases. True by default.

Return type

str

Returns

The “cleaned” path, e.g. The path, e.g. /home/user/documents

classmethod script_filename(level=1)[source]

Returns the file name of the calling method

Parameters

level – The stack level relative to this function, for internal use only. (+1 = caller, +2 = caller’s caller etc.)

Return type

str

Returns

The absolute filename of the script file

classmethod script_path(level=1)[source]

Returns the file name of the calling method

Parameters

level – The stack level relative to this function, for internal use only. (+1 = caller, +2 = caller’s caller etc.)

Return type

str

Returns

The absolute filename of the script file

classmethod split_ext(filename)[source]

Returns the extension and file component of given file path

As of now just a wrapper of os.path.split_ext().

Parameters

filename (str) – The filename

Return type

tuple[str, str]

Returns

Tuple of filename and the file extension (e.g. (“image”, “.png”)

classmethod split_path_components(path_name)[source]

Returns the single path components as a list

As of now just a wrapper of os.path.split().

Parameters

path_name (str) – The filename or dir name

Return type

list[str]

Returns

The single path components

SEP = '/'

The OS specific path separator