Back to bricks list
Introduction Version

FileHelper

Class containing only classmethod to simplify file management

Functions

CLEAN_EXTENSION - @classmethod

Return the extension of a file without the '.' if it is present on first caracter

Parameters:
NameTypeDefault valueDescription
extensionstr Extension of the file
Return type:
str
COPY_DIR - @classmethod

Copy a directory from source to destination

Parameters:
NameTypeDefault valueDescription
source_pathUnion Source directory path pathtype
destination_pathUnion Destination directory path pathtype
COPY_DIR_CONTENT_TO_DIR - @classmethod

Copy the content of a directory to another directory

Parameters:
NameTypeDefault valueDescription
source_dir_pathUnion Source directory path pathtype
destination_dir_pathUnion Destination directory path pathtype
COPY_FILE - @classmethod

Copy a file from source to destination

Parameters:
NameTypeDefault valueDescription
source_pathUnion Source file path pathtype
destination_pathUnion Destination file path pathtype
COPY_NODE - @classmethod

Copy a file or a directory from source to destination

Parameters:
NameTypeDefault valueDescription
source_pathUnion Source file or directory path pathtype
destination_pathUnion Destination file or directory path pathtype
CREATE_DIR_IF_NOT_EXIST - @classmethod

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

Parameters:
NameTypeDefault valueDescription
dir_pathUnion Path to the directory pathtype
Return type:
Path
CREATE_EMPTY_FILE_IF_NOT_EXIST - @classmethod

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

Parameters:
NameTypeDefault valueDescription
file_pathUnion Path to the file pathtype
Return type:
Path
CREATE_FILE_RESPONSE - @classmethod

Create a FastAPI FileResponse from a file path

:raises FileNotFoundError: if the file does not exist

Parameters:
NameTypeDefault valueDescription
file_pathUnion Path to the file pathtype
filenamestr Name of the file, defaults to none , optional
media_typestr Media type of the file, defaults to none , optional
Return type:
FileResponse
DELETE_DIR - @classmethod

Delete a directory and all its content

Parameters:
NameTypeDefault valueDescription
dir_pathUnion Path to the directory pathtype
ignore_errorsbool True If true, do not raise an exception if the directory does not exist, defaults to true , optional
DELETE_DIR_CONTENT - @classmethod

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

Parameters:
NameTypeDefault valueDescription
dir_pathUnion Path to the directory to empty pathtype
ignore_errorsbool True If true, do not raise an exception if the directory does not exist, defaults to true , optional
DELETE_FILE - @classmethod

Delete a file

Parameters:
NameTypeDefault valueDescription
file_pathUnion Path to the file pathtype
ignore_errorsbool True If true, do not raise an exception if the file does not exist, defaults to true , optional
DELETE_NODE - @classmethod

Delete a file or a directory

Parameters:
NameTypeDefault valueDescription
node_pathUnion Path to the file or directory pathtype
ignore_errorsbool True If true, do not raise an exception if the node does not exist, defaults to true , optional
DETECT_FILE_ENCODING - @classmethod

Detect the encoding of a file using charset-normalizer.

Parameters:
NameTypeDefault valueDescription
file_pathUnion Path to the file pathtype
default_encodingstr utf-8 Default encoding to use if the encoding is not detected, defaults to 'utf-8' , optional
Return type:
str
EXISTS_ON_OS - @classmethod

Exist on local machine

Parameters:
NameTypeDefault valueDescription
pathUnion
GET_DIR - @classmethod

Return the parent directory of a file or a folder

Parameters:
NameTypeDefault valueDescription
pathUnion Path to the file or folder pathtype
Return type:
Path
GET_DIR_CONTENT_AS_JSON - @classmethod

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

Parameters:
NameTypeDefault valueDescription
pathUnion Path to the directory pathtype
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.

Parameters:
NameTypeDefault valueDescription
pathUnion Path to the file or folder pathtype
GET_EXTENSION - @classmethod

Return the extension of a file without the '.'. Return None if no extension is found (like folder).

Parameters:
NameTypeDefault valueDescription
pathUnion Path to the file or folder pathtype
Return type:
str
GET_EXTENSION_FROM_CONTENT_TYPE - @classmethod

Return the extension of a file from its content type

Parameters:
NameTypeDefault valueDescription
content_typestr 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

Parameters:
NameTypeDefault valueDescription
sizefloat Size in bytes
Return type:
str
GET_MIME - @classmethod
Parameters:
NameTypeDefault valueDescription
pathUnion
Return type:
str
GET_NAME - @classmethod

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

Parameters:
NameTypeDefault valueDescription
pathUnion Path to the file or folder pathtype
GET_NAME_WITH_EXTENSION - @classmethod

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

Parameters:
NameTypeDefault valueDescription
pathUnion Path to the file or folder pathtype
GET_PATH - @classmethod

Return a Path object from a string or a Path object

Parameters:
NameTypeDefault valueDescription
pathUnion Path to convert pathtype
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).

Parameters:
NameTypeDefault valueDescription
pathUnion Path to the file or folder pathtype
Return type:
int
IS_AUDIO - @classmethod
Parameters:
NameTypeDefault valueDescription
pathUnion
Return type:
bool
IS_CSV - @classmethod
Parameters:
NameTypeDefault valueDescription
pathUnion
Return type:
bool
IS_DIR - @classmethod
Parameters:
NameTypeDefault valueDescription
pathUnion
Return type:
bool
IS_FILE - @classmethod
Parameters:
NameTypeDefault valueDescription
pathUnion
Return type:
bool
IS_IMAGE - @classmethod
Parameters:
NameTypeDefault valueDescription
pathUnion
Return type:
bool
IS_JPG - @classmethod
Parameters:
NameTypeDefault valueDescription
pathUnion
Return type:
bool
IS_JSON - @classmethod
Parameters:
NameTypeDefault valueDescription
pathUnion
Return type:
bool
IS_PNG - @classmethod
Parameters:
NameTypeDefault valueDescription
pathUnion
Return type:
bool
IS_TXT - @classmethod
Parameters:
NameTypeDefault valueDescription
pathUnion
Return type:
bool
MOVE_FILE_OR_DIR - @classmethod

Move a file or a directory from source to destination

Parameters:
NameTypeDefault valueDescription
source_pathUnion Source file or directory path pathtype
destination_pathUnion Destination file or directory path pathtype
SANITIZE_NAME - @classmethod

Sanitize a file name, folder name or path in order to prevent injection when using the file name Basically, it keeps only the alphanumeric characters and -,_,,/

Parameters:
NameTypeDefault valueDescription
namestr 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.

Parameters:
NameTypeDefault valueDescription
fs_node_namestr Name of the new fs node
dir_pathstr 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.

Parameters:
NameTypeDefault valueDescription
list_fs_node_namesList List of existing fs node names [str]
fs_node_namestr Name of the new fs node
Return type:
str