core.files.fileutils

class core.files.fileutils.PersistentList(filename, timeout=0, concurrent=True)[source]

Bases: list

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.

class core.files.fileutils.filegen(arg: int | str = 0, tmpdir: Path | None = None, lock_timeout: int = 0, if_exists: Literal['skip', 'overwrite', 'backup', 'error'] = 'error', verbose: bool = True)[source]

Bases: object

core.files.fileutils.get_git_commit()[source]
core.files.fileutils.mdir(directory: Path | str, mdir_filename: str = 'mdir.json', strict: bool = False, create: bool = True, **kwargs) Path[source]

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)

core.files.fileutils.safe_move(src, dst, makedirs=True)[source]

Move src file to dst

if makedirs: create directory if necessary

core.files.fileutils.skip(filename: Path, if_exists: str = 'skip')[source]

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’…

core.files.fileutils.temporary_copy(src: Path, enable: bool = True, **kwargs)[source]

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

Parameters:
  • src (Path) – Path to the source file/folder to copy.

  • enable (bool) – whether to enable the copy, otherwise returns the input

  • TemporaryDirectory (Other **kwargs are passed to) –

Yields:

Path – Path to the temporary file/folder