Commons

treefiles.commons.join(*paths: Union[treefiles.tree.S, treefiles.tree.T, str]) treefiles.tree.S

Wrapper of os.path.join

treefiles.commons.isDir(path: Union[treefiles.tree.S, treefiles.tree.T, str])

Wrapper of os.path.isdir

treefiles.commons.load_yaml(fname: str, **kwargs)

Loads a yaml file with yaml.load

treefiles.commons.dump_yaml(fname: str, data, **kwargs)

Dumps a dict to a yaml file with yaml.dump

treefiles.commons.load_json(filename: str, force_ext: bool = True, **kwargs)

Loads a json file with json.load

Returns

dict

treefiles.commons.dump_json(filename: str, data, force_ext: bool = True, **kwargs)

Dumps a dict to a json file with json.dump

Parameters

data – dict

treefiles.commons.pprint_json(data: dict)

Returns a pretty printed dict

treefiles.commons.curDir(file: Optional[str] = None)

Absolute path of current directory

treefiles.commons.curDirs(file: str, *paths: str) treefiles.tree.S

Absolute path of current directory joined with *paths

treefiles.commons.removeIfExists(fname: str)

Remove fname if it exists

treefiles.commons.remove(*args: str)

Remove files in globs *args if they exist

treefiles.commons.move(arg: str, dest: str)

Move files in glob arg to dest with shutil.move

treefiles.commons.copyfile(arg: str, dest: str)

Copy a file in glob arg to dest with shutil.copyfile

dest is a directory path, later joined with args’ basenames

treefiles.commons.copyFile(src: str, dst: str)

Copy a file src to dst with shutil.copyfile

dst is a file, the file path of the copied file

Will create a sym link from in_fname to the directory out_dir :param in_fname str: Filename of the file :param out_dir Tree: Tree instance representing the directory where to save the link :return: filename of the created link

treefiles.commons.find_new_dir(temp: str, start=0)

Return new directory name indexed as the first start index available in temp formattable path

treefiles.commons.natural_sort(l: List[str]) List[str]

Sorts a list of paths in a natural way

treefiles.commons.listdir(root: Union[treefiles.tree.T, str]) List[treefiles.tree.S]

Returns a list of files and folders in directory

treefiles.commons.load_zip(file_name: str)

Extract a zip file

Parameters

file_name – path to the zip file

treefiles.commons.dump_zip(file_name: str, paths: Union[str, treefiles.tree.Tree, List[str]], name: Optional[str] = None)

Writes a bunch of files to a zip file

Parameters
  • file_name – path to the zip file

  • paths – either a directory path (in this case all files under it are included in the zip file), or a list of files. The tree structure is respected only if a directory path is given. If a list is given, all files are zipped under the common directory name name.

  • name – common directory name in zip file

treefiles.commons.get_timestamp(fmt: str = '%d%m%Y_%H%M%S') str

Return timestamp: 25122021_151240

treefiles.commons.insert_before(src: str, sub: str, data) str
>>> insert_before("long_filename.mha", ".mha", "_newfile")
long_filename_newfile.mha
treefiles.commons.flatten_dict(d, parent_key='', sep='_')

Flatten a dictionnary

class treefiles.commons.no_stdout(enable: bool)
Usage:
with tf.no_stdout(is_enabled: bool):

treefiles.commons.none(var, default_value)

Quick ‘if is None’

treefiles.commons.get_iterable(x)

Ensure x is iterable

Print a link in PyCharm to a line in file Defaults to line where this function was called

Print a link in PyCharm to a module, function, class, method or property