filesystem#
- pythonning.filesystem.copy_path_to(path: Path, target_path: Path)#
Create a copy of the given filesystem object at the given path.
Directory are recursively copied and files have their metadata preserved.
For more complex behavior (symlink, …) you can copy this function and modify it for your need.
- Parameters:
path – Filesystem path to an existing file or directory
target_path – Filesystem path to an existing file or directory, of the same type as the path argument.
- pythonning.filesystem.copyfile(src_file: Path, target_path: Path, callback: Callable[[int, int, int], None] | None = None, chunk_size: int = 1048576, use_cache: bool = False) Path #
Copy src file to target and preserve file stats.
Similar to
shutil.copy2()
but with:a callback parameter: Callback is only useful for file above few MB as is it not called enough often for smaller files.
a cache option: for files on slow network that are often accessed. Note the lifetime of the cache is not guaranteed as being stored on the system default temporary location.
If you know your source is a symlink and you would like to copy it to target as a symlink then use
shutil.copy(follow_symlinks=False)
instead.- Parameters:
src_file – filesystem path to an existing file
target_path – filesystem path to a non-existing file or an existing directory.
callback – function called on each chunk of the file read. Signature is : (“current chunk”, “chunk size”, “total size”) -> None all values expressed in bytes.
chunk_size – size in bytes of each chunk of the file to read. Must not be bigger than the file size itself. Lower value increase the number of calls to callback but slow the process.
use_cache – True to store and retrieve the file from a local cache. Useful when the file is stored on slow network locations. The cache is preserved between sessions. Note if the cache doesn’t exist the first time this would imply 2 copy operation, one to create the cache, and one to copy to target_path. Both would call the callback making the total 2 times the size of the src_file.
- Returns:
target_path
- pythonning.filesystem.copytree(src_dir: Path, target_dir: Path, callback: Callable[[Path, int, int], None], **kwargs)#
Recursively copy a directory tree and return the destination directory.
Difference with
shutil.copytree
is the ability to use callback called on each path copied. Useful to display a progress bar for example.- Parameters:
src_dir – filesystem path to an existing directory
target_dir – filesystem path to an existing directory
callback – function called on each path copied with: (“path”, “path index”, “total number of paths”)
kwargs – passed to
shutil.copytree()
- pythonning.filesystem.extract_zip(zip_path: Path, remove_zip=True)#
Exract the given zip archive content in the directory it is in.
- Parameters:
zip_path – path to an existing zip file on the filesystem.
remove_zip – True to delete the zip once extracted
- Returns:
root directory the extracted file can be found at
- pythonning.filesystem.get_dir_content(src_dir: Path, recursive=True) List[Path] #
Return a list of paths this directory contains.
Return the whole files and directory tree if recursive=True. Be aware that recursive parsing can take some time for big file trees.
- Parameters:
src_dir – filesystem path to an existing directory
recursive – True to recursively process subdirectories
- Returns:
list of absolute existing paths to file and directories
- pythonning.filesystem.move_directory_content(src_directory: Path, target_directory: Path, exists_ok: bool = False, recursive: bool = True)#
Move (NOT a copy) all the files and directories in the source to the target.
Handle move between different disk roots.
- Parameters:
src_directory – filesystem path to an existing directory
target_directory – filesystem path to an existing directory
exists_ok – True to ignore if the target file already exists, else will raise en error.
recursive – True to also process all subdirectory recursively to not miss any files.
- pythonning.filesystem.rmtree(path: Path, ignore_errors=False)#
Remove the directory and its content while handling any potential PermissionError.
This function is copied from
tempfile.TemporaryDirectory._rmtree
- Parameters:
path – filesystem path to an existing directory
ignore_errors – do not raise if there is error during cleanup
- pythonning.filesystem.set_path_read_only(path: Path)#
Remove write permissions for everyone on the given file.
Does not touch other permissions.
References