fs¶
Functions
|
Create tar archive from directory and optionally split it into parts of specified size. |
|
Change directory name in path by index. |
|
Recursively delete a directory tree, but save root directory. |
|
|
|
Copy file from one path to another, if destination directory doesn't exist it will be created. |
|
Check whether directory is empty or not. |
|
Check whether directory exists or not. |
|
Generator that yields paths to directories that meet the requirements of the check_function. |
|
Generator that yields paths to directories that contain markers files. |
|
Load image from url to host by target path. |
|
Recursively create parent directory for target path. |
|
Check whether file exists or not. |
|
Get the size of a directory. |
|
Extracts file extension from a given path. |
|
Get hash from target file. |
|
Extracts file name from a given path. |
|
Extracts file name with ext from a given path. |
|
Get the size of a file. |
|
Get list containing the names of the directories in the given directory. |
|
Creates a hard link pointing to src named dst. |
|
Creates a hard links pointing to src named dst files recursively. |
|
|
|
Recursively walks through directory and returns list with all file paths. |
|
Returns list with file paths presented in given directory. |
|
Recursively walks through directory and returns list with all file paths. |
|
Get tree for target directory and displays it in the log. |
|
Creates a leaf directory and all intermediate ones. |
|
|
|
Recursively delete a directory tree. |
|
Cleans the given directory from junk files and dirs (e.g. |
|
Remove file which may not exist. |
|
Returns integer representation of byte size from string representation. |
|
Sets access and modification times for a file. |
|
Get tree for target directory. |
|
Unpacks archive to the target directory, removes junk files and directories. |
Description
-
archive_directory(dir_, tar_path, split=
None
)[source]¶ Create tar archive from directory and optionally split it into parts of specified size.
- Parameters
- Returns
None or list of archive parts if split is not None
- Return type
Union[None, List[str]]
- Usage example
from supervisely.io.fs import archive_directory # If split is not needed. archive_directory('/home/admin/work/projects/examples', '/home/admin/work/examples.tar') # If split is specified. archive_parts_paths = archive_directory('/home/admin/work/projects/examples', '/home/admin/work/examples/archive.tar', split=1000000) print(archive_parts_paths) # ['/home/admin/work/examples/archive.tar.001', '/home/admin/work/examples/archive.tar.002']
- change_directory_at_index(path, dir_name, dir_index)[source]¶
Change directory name in path by index. If you use counting from the end, keep in mind that if the path ends with a file, the file will be assigned to the last index.
- Parameters
- Returns
New path
- Return type
- Raises
IndexError – If the catalog index is out of bounds for a given path
- Usage example
import supervisely as sly input_path = 'head/dir_1/file.txt' new_path = sly.io.fs.change_directory_at_index(input_path, 'dir_2', -2) print(new_path)
-
clean_dir(dir_, ignore_errors=
True
)[source]¶ Recursively delete a directory tree, but save root directory.
- Parameters
- dir : str
Target directory path.
- Ignore_errors
Ignore possible errors while removes directory content.
- Ignore_errors
bool
- Returns
None
- Return type
NoneType
- Usage example
from supervisely.io.fs import clean_dir clean_dir('/home/admin/work/projects/examples')
- copy_file(src, dst)[source]¶
Copy file from one path to another, if destination directory doesn’t exist it will be created.
- dirs_filter(input_path, check_function)[source]¶
Generator that yields paths to directories that meet the requirements of the check_function.
- Parameters
- input_path : str
path to the directory in which the search will be performed
- check_function : Callable
function to check that directory meets the requirements and returns bool
- Usage example
import supervisely as sly input_path = '/home/admin/work/projects/examples' # Prepare the check function. def check_function(directory) -> bool: images_dir = os.path.join(directory, "images") annotations_dir = os.path.join(directory, "annotations") return os.path.isdir(images_dir) and os.path.isdir(annotations_dir) for directory in sly.fs.dirs(input_path, check_function): # Now you can be sure that the directory meets the requirements. # Do something with it. print(directory)
- Return type
-
dirs_with_marker(input_path, markers, check_function=
None
, ignore_case=False
)[source]¶ Generator that yields paths to directories that contain markers files. If the check_function is specified, then the markered directory will be yielded only if the check_function returns True. The check_function must take a single argument - the path to the markered directory and return True or False.
- Parameters
- input_path : str
path to the directory in which the search will be performed
- markers : Union[str, List[str]]
single marker or list of markers (e.g. ‘config.json’ or [‘config.json’, ‘config.yaml’])
- check_function : Callable
function to check that directory meets the requirements and returns bool
- ignore_case : bool
ignore case when searching for markers
- Usage example
import supervisely as sly input_path = '/home/admin/work/projects/examples' # You can pass a string if you have only one marker. # markers = 'config.json' # Or a list of strings if you have several markers. # There's no need to pass one marker in different cases, you can use ignore_case=True for this. markers = ['config.json', 'config.yaml'] # Check function is optional, if you don't need the directories to meet any requirements, # you can omit it. def check_function(dir_path): test_file_path = os.path.join(dir_path, 'test.txt') return os.path.exists(test_file_path) for directory in sly.fs.dirs_with_marker(input_path, markers, check_function, ignore_case=True): # Now you can be sure that the directory contains the markers and meets the requirements. # Do something with it. print(directory)
- Return type
-
download(url, save_path, cache=
None
, progress=None
, headers=None
, timeout=None
)[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
An instance of FileCache class that provides caching functionality for the downloaded content. If None, caching is disabled.
- progress : Progress, optional
Function for tracking download progress.
- headers : Dict, optional.
A dictionary of HTTP headers to include in the request.
- timeout : int, optional.
The maximum number of seconds to wait for a response from the server. If the server does not respond within the timeout period, a TimeoutError is raised.
- Returns
Full path to downloaded image
- Return type
- Usage example
from supervisely.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 # if you need to specify some headers headers = { 'User-Agent': 'Mozilla/5.0', } im_path = download(img_link, '/home/admin/work/projects/examples/avatar.jpeg', headers=headers) print(im_path) # Output: # /home/admin/work/projects/examples/avatar.jpeg
- ensure_base_path(path)[source]¶
Recursively create parent directory for target path.
- Parameters
- path : str
Target dir path.
- Returns
None
- Return type
NoneType
- Usage example
from supervisely.io.fs import ensure_base_path ensure_base_path('/home/admin/work/projects/example')
- get_subdirs(dir_path)[source]¶
Get list containing the names of the directories in the given directory.
- hardlink_or_copy_file(src, dst)[source]¶
Creates a hard link pointing to src named dst. If the link cannot be created, the file will be copied.
- hardlink_or_copy_tree(src, dst)[source]¶
Creates a hard links pointing to src named dst files recursively. If the link cannot be created, the file will be copied.
-
list_dir_recursively(dir, include_subdirs=
False
, use_global_paths=False
)[source]¶ Recursively walks through directory and returns list with all file paths.
- Parameters
- Returns
List containing file paths.
- Return type
List[str]
- Usage example
import supervisely as sly 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']
-
list_files(dir, valid_extensions=
None
, filter_fn=None
, ignore_valid_extensions_case=False
)[source]¶ Returns list with file paths presented in given directory.
- Parameters
- dir :
str
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.
- ignore_valid_extensions_case : bool
If True, valid extensions will be compared with file extensions in case-insensitive manner.
- dir :
- Returns
List with file paths
- Return type
List[str]
- Usage example
import supervisely as sly 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']
-
list_files_recursively(dir, valid_extensions=
None
, filter_fn=None
)[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
import supervisely as sly 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']
-
log_tree(dir_path, logger, level=
'info'
)[source]¶ Get tree for target directory and displays it in the log.
- Parameters
- dir_path : str
Target directory path.
- logger : logger
Logger to display data.
- Returns
None
- Return type
NoneType
- Usage example
from supervisely.io.fs import log_tree logger = sly.logger log_tree('/home/admin/work/projects/examples', logger)
-
mkdir(dir, remove_content_if_exists=
False
)[source]¶ Creates a leaf directory and all intermediate ones.
- Parameters
- dir :
str
Target dir path.
- dir
str
- dir :
- Remove_content_if_exists
Remove directory content if it exist.
- Remove_content_if_exists
bool
- Returns
None
- Return type
NoneType
- Usage example
from supervisely.io.fs import mkdir mkdir('/home/admin/work/projects/example')
- remove_dir(dir_)[source]¶
Recursively delete a directory tree.
- Parameters
- dir : str
Target directory path.
- Returns
None
- Return type
NoneType
- Usage example
from supervisely.io.fs import remove_dir remove_dir('/home/admin/work/projects/examples')
- remove_junk_from_dir(dir)[source]¶
Cleans the given directory from junk files and dirs (e.g. .DS_Store, __MACOSX, Thumbs.db, etc.).
- silent_remove(file_path)[source]¶
Remove file which may not exist.
- Parameters
- file_path : str
File path.
- Returns
None
- Return type
NoneType
- Usage example
from supervisely.io.fs import silent_remove silent_remove('/home/admin/work/projects/examples/1.jpeg')
- string_to_byte_size(string)[source]¶
- Returns integer representation of byte size from string representation.
If input is integer, returns the same integer for convenience.
- param string
string representation of byte size (e.g. 1.5Kb, 2Mb, 3.7Gb, 4.2Tb) or integer
- type string
Union[str, int]
- return
integer representation of byte size (or the same integer if input is integer)
- rtype
int
- raises ValueError
if input string is invalid
- Usage example
- Return type
- touch(path)[source]¶
Sets access and modification times for a file.
- Parameters
- path : str
Target file path.
- Returns
None
- Return type
NoneType
- Usage example
from supervisely.io.fs import touch touch('/home/admin/work/projects/examples/1.jpeg')
- tree(dir_path)[source]¶
Get tree for target directory.
- Parameters
- dir_path : str
Target directory path.
- Returns
Tree with directory files and subdirectories
- Return type
- Usage example
from supervisely.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
-
unpack_archive(archive_path, target_dir, remove_junk=
True
)[source]¶ Unpacks archive to the target directory, removes junk files and directories.
- Parameters
- Returns
None
- Return type
NoneType
- Usage example
import supervisely as sly archive_path = '/home/admin/work/examples.tar' target_dir = '/home/admin/work/projects' sly.fs.unpack_archive(archive_path, target_dir)