Menu
Introduction
Getting Started
Developer guide
Release
Architecture
CLI
Technical documentations
Version
Publication date

Mar 9, 2022

Confidentiality
Public
Reactions
1
Share

FileHelper

Class containing only classmethod to simplify file management

Functions
clean_extension @classmethod

TODO deprecated, to remove in v0.19.0 Return the extension of a file without the '.' if it is present on first caracter

extension : str
extension of the file
Return type : str
copy_dir @classmethod

Copy a directory from source to destination

source_path : str | pathlib.Path
source directory path
destination_path : str | pathlib.Path
destination directory path
copy_dir_content_to_dir @classmethod

Copy the content of a directory to another directory

source_dir_path : str | pathlib.Path
source directory path
destination_dir_path : str | pathlib.Path
destination directory path
copy_file @classmethod

Copy a file from source to destination

source_path : str | pathlib.Path
source file path
destination_path : str | pathlib.Path
destination file path
copy_node @classmethod

Copy a file or a directory from source to destination

source_path : str | pathlib.Path
source file or directory path
destination_path : str | pathlib.Path
destination file or directory path
create_dir_if_not_exist @classmethod

Create a directory and all intermediate directories if they do not exist

dir_path : str | pathlib.Path
path to the directory
Return type : Path
create_empty_file_if_not_exist @classmethod

Create an empty file if it does not exist. Creates intermediate directories if needed.

file_path : str | pathlib.Path
path to the file
Return type : Path
create_file_response @classmethod

Create a FastAPI FileResponse from a file path

file_path : str | pathlib.Path
path to the file
filename : str
name of the file
media_type : str
media type of the file
content_disposition_type : Literal - attachment
Return type : FileResponse
delete_dir @classmethod

Delete a directory and all its content

dir_path : str | pathlib.Path
path to the directory
ignore_errors : bool - True
if True, do not raise an exception if the directory does not exist
delete_dir_content @classmethod

Delete all the content of a directory but not the directory itself.

dir_path : str | pathlib.Path
path to the directory to empty
ignore_errors : bool - True
if True, do not raise an exception if the directory does not exist
delete_file @classmethod

Delete a file

file_path : str | pathlib.Path
path to the file
ignore_errors : bool - True
if True, do not raise an exception if the file does not exist
delete_node @classmethod

Delete a file or a directory

node_path : str | pathlib.Path
path to the file or directory
ignore_errors : bool - True
if True, do not raise an exception if the node does not exist
detect_file_encoding @classmethod

Detect the encoding of a file using charset-normalizer.

file_path : str | pathlib.Path
path to the file
default_encoding : str - utf-8
default encoding to use if the encoding is not detected
Return type : str
exists_on_os @classmethod

Exist on local machine

path : str | pathlib.Path
get_dir @classmethod

Return the parent directory of a file or a folder

path : str | pathlib.Path
path to the file or folder
Return type : Path
get_dir_content_as_json @classmethod

Return the content of a directory as a json nested object.

path : str | pathlib.Path
path to the directory
Return type : Any
get_dir_name @classmethod

Return the name of the folder. If the path is a file, return the name of the parent folder. If the path is a folder, return the name of the folder.

path : str | pathlib.Path
path to the file or folder
get_extension_from_content_type @classmethod

Return the extension of a file from its content type

content_type : str
content type of the file
Return type : str
get_file_size_pretty_text @classmethod

Return a human readable size from a size in bytes. Ex : 1024 -> 1 KB

size : float
size in bytes
Return type : str
get_mime @classmethod
path : str | pathlib.Path
Return type : str
get_name_without_extension @classmethod

Return the name of the file without the extension or the name of the folder. For compound extensions like .tar.gz, removes all extension parts.

path : str | pathlib.Path
path to the file or folder
Return type : str
get_node_name @classmethod

Return the name of the file with the extension or the name of the folder

path : str | pathlib.Path
path to the file or folder
get_normalized_extension @classmethod

Return the extension of a file without the '.' in lowercase. Return None if no extension is found (like folder). Special cases:

  • .R extension is returned as uppercase 'R'
  • Compound extensions like .tar.gz are returned as 'tar.gz'
path : str | pathlib.Path
path to the file or folder
Return type : str | None
get_path @classmethod

Return a Path object from a string or a Path object

path : str | pathlib.Path
path to convert
Return type : Path
get_size @classmethod

Return the size of a file or a folder. For folder, it is the sum of the size of all files in the folder (recursive).

path : str | pathlib.Path
path to the file or folder
Return type : int
is_audio @classmethod
path : str | pathlib.Path
Return type : bool
is_csv @classmethod
path : str | pathlib.Path
Return type : bool
is_dir @classmethod
path : str | pathlib.Path
Return type : bool
is_file @classmethod
path : str | pathlib.Path
Return type : bool
is_image @classmethod
path : str | pathlib.Path
Return type : bool
is_jpg @classmethod
path : str | pathlib.Path
Return type : bool
is_json @classmethod
path : str | pathlib.Path
Return type : bool
is_png @classmethod
path : str | pathlib.Path
Return type : bool
is_txt @classmethod
path : str | pathlib.Path
Return type : bool
is_video @classmethod
path : str | pathlib.Path
Return type : bool
move_file_or_dir @classmethod

Move a file or a directory from source to destination

source_path : str | pathlib.Path
source file or directory path
destination_path : str | pathlib.Path
destination file or directory path
normalize_extension @classmethod

Normalize a file extension by removing leading dots and applying case rules. Returns the extension in lowercase except for special cases (.R stays uppercase). Returns None if the input is None.

extension : str
extension to normalize (with or without leading dot)
Return type : str | None
sanitize_name @classmethod

Sanitize a file name, folder name or path in order to prevent injection when using the file name Removes dangerous characters and prevents path traversal attacks Also normalizes file extensions (lowercase except for .R, handles .tar compounds)

name : str
name to sanitize
Return type : str
generate_unique_fs_node_for_dir @staticmethod

Generate a unique fs node name for a list of node names. Append _1, _2... before the extension if the str is already in the list. Useful to avoid name collision when adding a file to a directory.

fs_node_name : str
name of the new fs node
dir_path : str
path to the directory
Return type : str
generate_unique_fs_node_for_list @staticmethod

Generate a unique fs node name for a list of node names. Append _1, _2... before the extension if the str is already in the list. Useful to avoid name collision when adding a file to a directory.

list_fs_node_names : list
list of existing fs node names
fs_node_name : str
name of the new fs node
Return type : str
Shine Logo
Technical bricks to reuse or customize

Have you developed a brick?

Share it to accelerate projects for the entire community.