๐
Python Utils
On this page
String
Convert string to snake case
python
def snake_case(s):return '_'.join(sub('([A-Z][a-z]+)', r' \1',sub('([A-Z]+)', r' \1',s.replace('-', ' '))).split()).lower()
File
Find all files in the directory by the specified extension type
python
from glob import globdef find_files_by_extension(dir_path, extension):return glob(path.join(dir_path, "*.{}".format(extension)))
Get file name without the extension
python
from pathlib import Pathdef get_file_name_no_extension(path):return Path(path).stem
Create file directories if not exists
python
import osdef init_dir_paths():for path in [dir_path, processed_dir_path, unprocessed_dir_path]:if not os.path.exists(path):os.mkdir(path)print(f'Create directory {path}')