ostk.core.filesystem.Directory

class Directory

Bases: pybind11_object

Methods

contains_file_with_name

Check if the Directory contains a file with the specified name.

create

Create the Directory on the filesystem.

exists

Check if the Directory exists on the filesystem.

get_directories

Get all subdirectories in this Directory.

get_name

Get the name of the Directory.

get_parent_directory

Get the parent directory of this Directory.

get_path

Get the full path of the Directory.

is_defined

Check if the Directory is defined.

is_empty

Check if the Directory is empty.

path

Create a Directory from a path.

remove

Remove the Directory from the filesystem.

root

Get the root directory of the filesystem.

to_string

Convert the Directory to a string representation.

undefined

Create an undefined Directory.

contains_file_with_name(
self: ostk.core.filesystem.Directory,
name: ostk.core.type.String,
) bool

Check if the Directory contains a file with the specified name.

Parameters:

name (str) -- The name of the file to search for.

Returns:

True if a file with the given name exists in the directory.

Return type:

bool

Example

>>> from ostk.core.filesystem import Path
>>> directory = Directory.path(Path.parse("/home/user"))
>>> directory.contains_file_with_name("document.txt")  # True/False
create(
self: ostk.core.filesystem.Directory,
owner_permissions: ostk.core.filesystem.PermissionSet = PermissionSet.rwx(),
group_permissions: ostk.core.filesystem.PermissionSet = PermissionSet.rx(),
other_permissions: ostk.core.filesystem.PermissionSet = PermissionSet.rx(),
) None

Create the Directory on the filesystem.

Creates the directory and any necessary parent directories.

Parameters:
  • owner_permissions (PermissionSet, optional) -- Permissions for the owner. Defaults to PermissionSet.rwx().

  • group_permissions (PermissionSet, optional) -- Permissions for the group. Defaults to PermissionSet.rx().

  • other_permissions (PermissionSet, optional) -- Permissions for others. Defaults to PermissionSet.rx().

Raises:

RuntimeError -- If the directory cannot be created.

Example

>>> from ostk.core.filesystem import Path, PermissionSet
>>> directory = Directory.path(Path.parse("/tmp/new_directory"))
>>> directory.create()  # Uses default permissions
>>> directory.create(PermissionSet.rwx(), PermissionSet.rx(), PermissionSet.none())
exists(self: ostk.core.filesystem.Directory) bool

Check if the Directory exists on the filesystem.

Returns:

True if the directory exists, False otherwise.

Return type:

bool

Example

>>> from ostk.core.filesystem import Path
>>> directory = Directory.path(Path.parse("/home"))
>>> directory.exists()  # True (usually)
get_directories(
self: ostk.core.filesystem.Directory,
) list[ostk.core.filesystem.Directory]

Get all subdirectories in this Directory.

Returns:

A list of subdirectories.

Return type:

list[Directory]

Example

>>> from ostk.core.filesystem import Path
>>> directory = Directory.path(Path.parse("/home/user"))
>>> subdirs = directory.get_directories()
get_name(self: ostk.core.filesystem.Directory) ostk.core.type.String

Get the name of the Directory.

Returns:

The directory name (last component of the path).

Return type:

str

Example

>>> from ostk.core.filesystem import Path
>>> directory = Directory.path(Path.parse("/home/user/Documents"))
>>> directory.get_name()  # "Documents"
get_parent_directory(
self: ostk.core.filesystem.Directory,
) ostk.core.filesystem.Directory

Get the parent directory of this Directory.

Returns:

The parent directory.

Return type:

Directory

Example

>>> from ostk.core.filesystem import Path
>>> directory = Directory.path(Path.parse("/home/user/Documents"))
>>> parent = directory.get_parent_directory()
>>> parent.get_name()  # "user"
get_path(self: ostk.core.filesystem.Directory) ostk.core.filesystem.Path

Get the full path of the Directory.

Returns:

The complete path to the directory.

Return type:

Path

Example

>>> from ostk.core.filesystem import Path
>>> directory = Directory.path(Path.parse("/home/user"))
>>> path = directory.get_path()
>>> str(path)  # "/home/user"
is_defined(self: ostk.core.filesystem.Directory) bool

Check if the Directory is defined.

Returns:

True if the Directory is defined, False otherwise.

Return type:

bool

Example

>>> from ostk.core.filesystem import Path
>>> directory = Directory.path(Path.parse("/home/user"))
>>> directory.is_defined()  # True
is_empty(self: ostk.core.filesystem.Directory) bool

Check if the Directory is empty.

Returns:

True if the directory contains no files or subdirectories, False otherwise.

Return type:

bool

Example

>>> from ostk.core.filesystem import Path
>>> directory = Directory.path(Path.parse("/tmp"))
>>> directory.is_empty()  # Depends on contents
static path(path: ostk.core.filesystem.Path) ostk.core.filesystem.Directory

Create a Directory from a path.

Parameters:

path (Path) -- The path to the directory.

Returns:

A Directory object representing the specified path.

Return type:

Directory

Example

>>> from ostk.core.filesystem import Path
>>> path = Path.parse("/home/user")
>>> directory = Directory.path(path)
remove(self: ostk.core.filesystem.Directory) None

Remove the Directory from the filesystem.

Removes the directory and all its contents recursively.

Raises:

RuntimeError -- If the directory cannot be removed.

Example

>>> from ostk.core.filesystem import Path
>>> directory = Directory.path(Path.parse("/tmp/temp_directory"))
>>> directory.remove()
static root() ostk.core.filesystem.Directory

Get the root directory of the filesystem.

Returns:

The root directory ("/" on Unix systems).

Return type:

Directory

Example

>>> root = Directory.root()
>>> root.get_name()  # "/"
to_string(self: ostk.core.filesystem.Directory) ostk.core.type.String

Convert the Directory to a string representation.

Returns:

String representation of the directory path.

Return type:

str

Example

>>> from ostk.core.filesystem import Path
>>> directory = Directory.path(Path.parse("/home/user"))
>>> directory.to_string()  # "/home/user"
static undefined() ostk.core.filesystem.Directory

Create an undefined Directory.

Returns:

An undefined Directory object.

Return type:

Directory

Example

>>> undefined_dir = Directory.undefined()
>>> undefined_dir.is_defined()  # False