FS

supervisely_lib.io.fs.get_file_name(path: str) → str[source]

Extracts file name from a given path.

Parameters

path (str) – Path to file.

Returns

File name without extension

Return type

str

Usage example
file_name = sly.fs.get_file_name("/home/admin/work/projects/lemons_annotated/ds1/img/IMG_0748.jpeg")

print(file_name)
# Output: IMG_0748
supervisely_lib.io.fs.get_file_ext(path: str) → str[source]

Extracts file extension from a given path.

Parameters

path (str) – Path to file.

Returns

File extension without name

Return type

str

Usage example
file_ext = sly.fs.get_file_ext("/home/admin/work/projects/lemons_annotated/ds1/img/IMG_0748.jpeg")

print(file_ext)
# Output: .jpeg
supervisely_lib.io.fs.get_file_name_with_ext(path: str) → str[source]

Extracts file name with ext from a given path.

Parameters

path (str) – Path to file.

Returns

File name with extension

Return type

str

Usage example
file_name_ext = sly.fs.get_file_name_with_ext("/home/admin/work/projects/lemons_annotated/ds1/img/IMG_0748.jpeg")

print(file_name_ext)
# Output: IMG_0748.jpeg
supervisely_lib.io.fs.list_dir_recursively(dir: str) → List[str][source]

Recursively walks through directory and returns list with all file paths.

Parameters

path (str) – Path to directory.

Returns

List containing file paths.

Return type

List[str]

Usage example
list_dir = sly.fs.list_dir_recursively("/home/admin/work/projects/lemons_annotated/")

print(list_dir)
# Output: ['meta.json', 'ds1/ann/IMG_0748.jpeg.json', 'ds1/ann/IMG_4451.jpeg.json', 'ds1/img/IMG_0748.jpeg', 'ds1/img/IMG_4451.jpeg']
supervisely_lib.io.fs.list_files_recursively(dir: str, valid_extensions: Optional[List[str]] = None, filter_fn=None) → List[str][source]

Recursively walks through directory and returns list with all file paths.

param dir

Target dir path.

param dir

str

param valid_extensions

List with valid file extensions.

type valid_extensions

List[str]

param filter_fn

Function with a single argument that determines whether to keep a given file path.

type filter_fn

returns

List with file paths

rtype

List[str]

Usage example
list_files = sly.fs.list_files_recursively("/home/admin/work/projects/lemons_annotated/ds1/img/")

print(list_files)
# Output: ['/home/admin/work/projects/lemons_annotated/ds1/img/IMG_0748.jpeg', '/home/admin/work/projects/lemons_annotated/ds1/img/IMG_4451.jpeg']
supervisely_lib.io.fs.list_files(dir: str, valid_extensions: Optional[List[str]] = None, filter_fn=None) → List[str][source]

Returns list with file paths presented in given directory.

Parameters
  • dir – Target dir path.

  • dir – str

  • valid_extensions (List[str]) – List with valid file extensions.

  • filter_fn – Function with a single argument that determines whether to keep a given file path.

Returns

List with file paths

Return type

List[str]

Usage example
list_files = sly.fs.list_files("/home/admin/work/projects/lemons_annotated/ds1/img/")

print(list_files)
# Output: ['/home/admin/work/projects/lemons_annotated/ds1/img/IMG_0748.jpeg', '/home/admin/work/projects/lemons_annotated/ds1/img/IMG_4451.jpeg']
supervisely_lib.io.fs.mkdir(dir: str) → None[source]

Creates a leaf directory and all intermediate ones.

Parameters
  • dir – Target dir path.

  • dir – str

Returns

None

Return type

NoneType

Usage example
from supervisely_lib.io.fs import mkdir
mkdir('/home/admin/work/projects/example')
supervisely_lib.io.fs.ensure_base_path(path: str) → None[source]

Recursively create parent directory for target path.

Parameters

path (str) – Target dir path.

Returns

None

Return type

NoneType

Usage example
from supervisely_lib.io.fs import ensure_base_path
ensure_base_path('/home/admin/work/projects/example')
supervisely_lib.io.fs.copy_file(src: str, dst: str) → None[source]

Copy file from one path to another, if destination directory doesn’t exist it will be created.

Parameters
  • src (str) – Source file path.

  • dst (str) – Destination file path.

Returns

None

Return type

NoneType

Usage example
from supervisely_lib.io.fs import copy_file
copy_file('/home/admin/work/projects/example/1.png', '/home/admin/work/tests/2.png')

Creates a hard link pointing to src named dst. If the link cannot be created, the file will be copied.

Parameters
  • src (str) – Source file path.

  • dst (str) – Destination file path.

Returns

None

Return type

NoneType

Usage example
from supervisely_lib.io.fs import hardlink_or_copy_file
hardlink_or_copy_file('/home/admin/work/projects/example/1.png', '/home/admin/work/tests/link.txt')

Creates a hard links pointing to src named dst files recursively. If the link cannot be created, the file will be copied.

Parameters
  • src (str) – Source dir path.

  • dst (str) – Destination dir path.

Returns

None

Return type

NoneType

Usage example
from supervisely_lib.io.fs import hardlink_or_copy_tree
hardlink_or_copy_tree('/home/admin/work/projects/examples', '/home/admin/work/tests/links')
supervisely_lib.io.fs.dir_exists(dir: str) → bool[source]

Check whether directory exists or not.

Parameters

dir (str) – Target directory path.

Returns

True if directory exists, False otherwise.

Return type

bool

Usage example
from supervisely_lib.io.fs import dir_exists
dir_exists('/home/admin/work/projects/examples') # True
dir_exists('/home/admin/work/not_exist_dir') # False
supervisely_lib.io.fs.dir_empty(dir: str) → bool[source]

Check whether directory is empty or not.

Parameters

dir (str) – Target directory path.

Returns

True if directory is empty, False otherwise.

Return type

bool

Usage example
from supervisely_lib.io.fs import dir_empty
dir_empty('/home/admin/work/projects/examples') # False
supervisely_lib.io.fs.file_exists(path: str) → bool[source]

Check whether file exists or not.

Parameters

dir (str) – Target file path.

Returns

True if file exists, False otherwise.

Return type

bool

Usage example
from supervisely_lib.io.fs import file_exists
file_exists('/home/admin/work/projects/examples/1.jpeg') # True
file_exists('/home/admin/work/projects/examples/not_exist_file.jpeg') # False
supervisely_lib.io.fs.get_subdirs(dir_path: str) → list[source]

Get list containing the names of the directories in the given directory.

Parameters

dir_path (str) – Target directory path.

Returns

List containing directories names.

Return type

list

Usage example
from supervisely_lib.io.fs import get_subdirs
subdirs = get_subdirs('/home/admin/work/projects/examples')
print(subdirs)
# Output: ['tests', 'users', 'ds1']
supervisely_lib.io.fs.clean_dir(dir_: str) → None[source]

Recursively delete a directory tree, but save root directory.

Parameters

dir (str) – Target directory path.

Returns

None

Return type

NoneType

Usage example
from supervisely_lib.io.fs import clean_dir
clean_dir('/home/admin/work/projects/examples')
supervisely_lib.io.fs.remove_dir(dir_: str) → None[source]

Recursively delete a directory tree.

Parameters

dir (str) – Target directory path.

Returns

None

Return type

NoneType

Usage example
from supervisely_lib.io.fs import remove_dir
remove_dir('/home/admin/work/projects/examples')
supervisely_lib.io.fs.silent_remove(file_path: str) → None[source]

Remove file which may not exist.

Parameters

file_path (str) – File path.

Returns

None

Return type

NoneType

Usage example
from supervisely_lib.io.fs import silent_remove
silent_remove('/home/admin/work/projects/examples/1.jpeg')
supervisely_lib.io.fs.get_file_size(path: str) → int[source]

Get the size of a file.

Parameters

path (str) – File path.

Returns

File size in bytes

Return type

int

Usage example
from supervisely_lib.io.fs import get_file_size
file_size = get_file_size('/home/admin/work/projects/examples/1.jpeg') # 161665
supervisely_lib.io.fs.get_directory_size(dir_path: str) → int[source]

Get the size of a directory.

Parameters

path (str) – Target directory path.

Returns

Directory size in bytes

Return type

int

Usage example
from supervisely_lib.io.fs import get_directory_size
dir_size = get_directory_size('/home/admin/work/projects/examples') # 8574563
supervisely_lib.io.fs.archive_directory(dir_: str, tar_path: str) → None[source]

Create tar archive from directory.

Parameters
  • dir (str) – Target directory path.

  • tar_path (str) – Path for output tar archive.

Returns

None

Return type

NoneType

Usage example
from supervisely_lib.io.fs import archive_directory
archive_directory('/home/admin/work/projects/examples', '/home/admin/work/examples.tar')
supervisely_lib.io.fs.get_file_hash(path: str) → str[source]

Get hash from target file.

Parameters

path (str) – Target file path.

Returns

File hash

Return type

str

Usage example
from supervisely_lib.io.fs import get_file_hash
hash = get_file_hash('/home/admin/work/projects/examples/1.jpeg') # rKLYA/p/P64dzidaQ/G7itxIz3ZCVnyUhEE9fSMGxU4=
supervisely_lib.io.fs.tree(dir_path: str) → str[source]

Get tree for target directory.

Parameters

dir_path (str) – Target directory path.

Returns

Tree with directory files and subdirectories

Return type

str

Usage example
from supervisely_lib.io.fs import tree
dir_tree = tree('/home/admin/work/projects/examples')
print(dir_tree)
# Output: /home/admin/work/projects/examples
# ├── [4.0K]  1
# │   ├── [165K]  crop.jpeg
# │   ├── [169K]  fliplr.jpeg
# │   ├── [169K]  flipud.jpeg
# │   ├── [166K]  relative_crop.jpeg
# │   ├── [167K]  resize.jpeg
# │   ├── [169K]  rotate.jpeg
# │   ├── [171K]  scale.jpeg
# │   └── [168K]  translate.jpeg
# ├── [ 15K]  123.jpeg
# ├── [158K]  1.jpeg
# ├── [188K]  1.txt
# ├── [1.3M]  1.zip
# ├── [4.0K]  2
# ├── [ 92K]  acura.png
# ├── [1.2M]  acura_PNG122.png
# ├── [198K]  aston_martin_PNG55.png
# ├── [4.0K]  ds1
# │   ├── [4.0K]  ann
# │   │   ├── [4.3K]  IMG_0748.jpeg.json
# │   │   ├── [ 151]  IMG_0777.jpeg.json
# │   │   ├── [ 151]  IMG_0888.jpeg.json
# │   │   ├── [3.7K]  IMG_1836.jpeg.json
# │   │   ├── [8.1K]  IMG_2084.jpeg.json
# │   │   ├── [5.5K]  IMG_3861.jpeg.json
# │   │   ├── [6.0K]  IMG_4451.jpeg.json
# │   │   └── [5.0K]  IMG_8144.jpeg.json
# │   └── [4.0K]  img
# │       ├── [152K]  IMG_0748.jpeg
# │       ├── [210K]  IMG_0777.jpeg
# │       ├── [210K]  IMG_0888.jpeg
# │       ├── [137K]  IMG_1836.jpeg
# │       ├── [139K]  IMG_2084.jpeg
# │       ├── [145K]  IMG_3861.jpeg
# │       ├── [133K]  IMG_4451.jpeg
# │       └── [136K]  IMG_8144.jpeg
# ├── [152K]  example.jpeg
# ├── [2.4K]  example.json
# ├── [153K]  flip.jpeg
# ├── [ 65K]  hash1.jpeg
# ├── [ 336]  meta.json
# └── [5.4K]  q.jpeg
# 5 directories, 37 files
supervisely_lib.io.fs.log_tree(dir_path: str, logger) → None[source]

Get tree for target directory and displays it in the log.

Parameters
  • dir_path (str) – Target directory path.

  • dir_path – Logger to display data.

Returns

None

Return type

NoneType

Usage example
from supervisely_lib.io.fs import log_tree
logger = sly.logger
log_tree('/home/admin/work/projects/examples', logger)
supervisely_lib.io.fs.touch(path: str) → None[source]

Sets access and modification times for a file.

Parameters

path (str) – Target file path.

Returns

None

Return type

NoneType

Usage example
from supervisely_lib.io.fs import touch
touch('/home/admin/work/projects/examples/1.jpeg')
supervisely_lib.io.fs.download(url: str, save_path: str, cache: Optional[supervisely_lib.io.fs_cache.FileCache] = None, progress: Optional[supervisely_lib.task.progress.Progress] = None) → str[source]

Load image from url to host by target path.

Parameters
  • url (str) – Target file path.

  • url – The path where the file is saved.

  • cache (FileCache, optional) – FileCache.

  • progress (Progress, optional) – Function for tracking download progress.

Returns

Full path to downloaded image

Return type

str

Usage example
from supervisely_lib.io.fs import download
img_link = 'https://m.media-amazon.com/images/M/MV5BMTYwOTEwNjAzMl5BMl5BanBnXkFtZTcwODc5MTUwMw@@._V1_.jpg'
im_path = download(img_link, '/home/admin/work/projects/examples/avatar.jpeg')
print(im_path)
# Output:
# /home/admin/work/projects/examples/avatar.jpeg