files.fileutils

files.fileutils

Classes

Name Description
PersistentList A list that saves its content in filename on each modification. The extension

PersistentList

files.fileutils.PersistentList(filename, timeout=0, concurrent=True)

A list that saves its content in filename on each modification. The extension must be .json.

concurrent: whether to activate concurrent mode. In this mode, the file is also read before each access.

Functions

Name Description
get_git_commit Returns the current git commit hash, or a placeholder if it cannot be obtained.
mdir Create or access a managed directory with path directory
safe_move Move src file to dst
skip Utility function to check whether to skip an existing file
temporary_copy Context manager to copy a file/folder to a temporary directory.

get_git_commit

files.fileutils.get_git_commit()

Returns the current git commit hash, or a placeholder if it cannot be obtained. Captures both stdout and stderr to handle cases where the command fails (e.g., not a git repository).

mdir

files.fileutils.mdir(
    directory,
    mdir_filename='mdir.json',
    strict=False,
    create=True,
    **kwargs,
)

Create or access a managed directory with path directory Returns the directory path, so that it can be used in directories definition: dir_data = mdir(‘/path/to/data/’)

tag it with a file mdir.json, containing: - The creation date - The last access date - The python file and module that was run during access - The username - The current git commit if available - Any other kwargs, such as: - project - version - description - etc

mdir_filename: default=‘mdir.json’

strict: boolean False: metadata is updated True: metadata is checked or added (default) (remove file content to override)

create: whether directory is automatically created (default True)

safe_move

files.fileutils.safe_move(src, dst, makedirs=True, verbose=True)

Move src file to dst

if makedirs: create directory if necessary

skip

files.fileutils.skip(filename, if_exists='skip')

Utility function to check whether to skip an existing file

if_exists: ‘skip’: skip the existing file ‘error’: raise an error on existing file ‘overwrite’: overwrite existing file ‘backup’: move existing file to a backup ‘.1’, ’.2’…

temporary_copy

files.fileutils.temporary_copy(src, enable=True, **kwargs)

Context manager to copy a file/folder to a temporary directory.

Args: src (Path): Path to the source file/folder to copy. enable (bool): whether to enable the copy, otherwise returns the input Other **kwargs are passed to TemporaryDirectory

Yields: Path: Path to the temporary file/folder