fileio

zenml.io.fileio.append_file(file_path: str, file_contents: str) None[source]

Appends file_contents to file.

Parameters
  • file_path – Local path in filesystem.

  • file_contents – Contents of file.

zenml.io.fileio.convert_to_str(path: Union[bytes, str]) str[source]

Converts a PathType to a str using UTF-8.

zenml.io.fileio.copy(src: Union[bytes, str], dst: Union[bytes, str], overwrite: bool = False) None[source]

Copy a file from the source to the destination.

zenml.io.fileio.copy_dir(source_dir: str, destination_dir: str, overwrite: bool = False) None[source]

Copies dir from source to destination.

Parameters
  • source_dir – Path to copy from.

  • destination_dir – Path to copy to.

  • overwrite – Boolean. If false, function throws an error before overwrite.

zenml.io.fileio.create_dir_if_not_exists(dir_path: str) None[source]

Creates directory if it does not exist.

Parameters

dir_path (str) – Local path in filesystem.

zenml.io.fileio.create_dir_recursive_if_not_exists(dir_path: str) None[source]

Creates directory recursively if it does not exist.

Parameters

dir_path – Local path in filesystem.

zenml.io.fileio.create_file_if_not_exists(file_path: str, file_contents: str = '{}') None[source]

Creates file if it does not exist.

Parameters
  • file_path – Local path in filesystem.

  • file_contents – Contents of file.

zenml.io.fileio.file_exists(path: Union[bytes, str]) bool[source]

Returns True if the given path exists.

zenml.io.fileio.find_files(dir_path: Union[bytes, str], pattern: str) Iterable[str][source]

Find files in a directory that match pattern.

Parameters
  • dir_path – Path to directory.

  • pattern – pattern like *.png.

Yields

All matching filenames if found, else None.

zenml.io.fileio.get_grandparent(dir_path: str) str[source]

Get grandparent of dir.

Parameters

dir_path – Path to directory.

Returns

The input paths parents parent.

zenml.io.fileio.get_parent(dir_path: str) str[source]

Get parent of dir.

Parameters

dir_path (str) – Path to directory.

Returns

Parent (stem) of the dir as a string.

zenml.io.fileio.glob(pattern: Union[bytes, str]) List[Union[bytes, str]][source]

Return the paths that match a glob pattern.

zenml.io.fileio.is_dir(path: Union[bytes, str]) bool[source]

Returns whether the given path points to a directory.

zenml.io.fileio.is_remote(path: str) bool[source]

Returns True if path exists remotely.

Parameters

path – Any path as a string.

Returns

True if remote path, else False.

zenml.io.fileio.is_root(path: str) bool[source]

Returns true if path has no parent in local filesystem.

Parameters

path – Local path in filesystem.

Returns

True if root, else False.

zenml.io.fileio.list_dir(dir_path: str, only_file_names: bool = False) List[str][source]

Returns a list of files under dir.

Parameters
  • dir_path – Path in filesystem.

  • only_file_names – Returns only file names if True.

Returns

List of full qualified paths.

zenml.io.fileio.make_dirs(path: Union[bytes, str]) None[source]

Make a directory at the given path, recursively creating parents.

zenml.io.fileio.mkdir(path: Union[bytes, str]) None[source]

Make a directory at the given path; parent directory must exist.

zenml.io.fileio.move(source: str, destination: str, overwrite: bool = False) None[source]

Moves dir or file from source to destination. Can be used to rename.

Parameters
  • source – Local path to copy from.

  • destination – Local path to copy to.

  • overwrite – boolean, if false, then throws an error before overwrite.

zenml.io.fileio.open(path: Union[bytes, str], mode: str = 'r') Any[source]

Open a file at the given path.

zenml.io.fileio.remove(path: Union[bytes, str]) None[source]

Remove the file at the given path. Dangerous operation.

zenml.io.fileio.rename(src: Union[bytes, str], dst: Union[bytes, str], overwrite: bool = False) None[source]

Rename source file to destination file.

Parameters
  • src – The path of the file to rename.

  • dst – The path to rename the source file to.

  • overwrite – If a file already exists at the destination, this method will overwrite it if overwrite=`True` and raise a FileExistsError otherwise.

Raises

FileExistsError – If a file already exists at the destination and overwrite is not set to True.

zenml.io.fileio.resolve_relative_path(path: str) str[source]

Takes relative path and resolves it absolutely.

Parameters

path – Local path in filesystem.

Returns

Resolved path.

zenml.io.fileio.rm_dir(dir_path: str) None[source]

Deletes dir recursively. Dangerous operation.

Parameters

dir_path – Dir to delete.

zenml.io.fileio.stat(path: Union[bytes, str]) Any[source]

Return the stat descriptor for a given file path.

zenml.io.fileio.walk(top: Union[bytes, str], topdown: bool = True, onerror: Optional[Callable[[...], None]] = None) Iterable[Tuple[Union[bytes, str], List[Union[bytes, str]], List[Union[bytes, str]]]][source]

Return an iterator that walks the contents of the given directory.

Parameters
  • top – Path of directory to walk.

  • topdown – Whether to walk directories topdown or bottom-up.

  • onerror – Callable that gets called if an error occurs.

Returns

An Iterable of Tuples, each of which contain the path of the current directory path, a list of directories inside the current directory and a list of files inside the current directory.