ostk.core.filesystem.File¶
- class File¶
Bases:
pybind11_object
Methods
Create the File on the filesystem.
Check if the File exists on the filesystem.
Get the contents of the File as a string.
Get the file extension.
Get the name of the File.
Get the parent directory of the File.
Get the full path of the File.
Get the file permissions.
Check if the File is defined.
Move the File to a different directory.
Create a File from a file path.
Remove the File from the filesystem.
Convert the File to a string representation.
Create an undefined File.
- create(
- self: ostk.core.filesystem.File,
- owner_permissions: ostk.core.filesystem.PermissionSet = PermissionSet.rw(),
- group_permissions: ostk.core.filesystem.PermissionSet = PermissionSet.r(),
- other_permissions: ostk.core.filesystem.PermissionSet = PermissionSet.r(),
- owner_permissions: ostk.core.filesystem.PermissionSet = PermissionSet.rw(),
Create the File on the filesystem.
Creates the file if it doesn't exist, including any necessary parent directories.
- Parameters:
owner_permissions (PermissionSet, optional) -- Permissions for the owner. Defaults to PermissionSet.rw().
group_permissions (PermissionSet, optional) -- Permissions for the group. Defaults to PermissionSet.r().
other_permissions (PermissionSet, optional) -- Permissions for others. Defaults to PermissionSet.r().
- Raises:
RuntimeError -- If the file cannot be created.
Example
>>> from ostk.core.filesystem import Path, PermissionSet >>> file = File.path(Path.parse("/tmp/new_file.txt")) >>> file.create() # Uses default permissions >>> file.create(PermissionSet.rw(), PermissionSet.r(), PermissionSet.none())
- exists(self: ostk.core.filesystem.File) bool ¶
Check if the File exists on the filesystem.
- Returns:
True if the file exists, False otherwise.
- Return type:
Example
>>> from ostk.core.filesystem import Path >>> file = File.path(Path.parse("/etc/passwd")) >>> file.exists() # True (on Unix systems) >>> nonexistent = File.path(Path.parse("/nonexistent/file.txt")) >>> nonexistent.exists() # False
- get_contents(self: ostk.core.filesystem.File) ostk.core.type.String ¶
Get the contents of the File as a string.
- Returns:
The file contents.
- Return type:
- Raises:
RuntimeError -- If the file cannot be read or doesn't exist.
Example
>>> from ostk.core.filesystem import Path >>> file = File.path(Path.parse("/path/to/text_file.txt")) >>> content = file.get_contents() >>> print(content) # File contents as string
- get_extension(self: ostk.core.filesystem.File) ostk.core.type.String ¶
Get the file extension.
- Returns:
The file extension (without the dot).
- Return type:
Example
>>> from ostk.core.filesystem import Path >>> file = File.path(Path.parse("/path/to/document.pdf")) >>> file.get_extension() # "pdf" >>> file_no_ext = File.path(Path.parse("/path/to/README")) >>> file_no_ext.get_extension() # ""
- get_name(
- self: ostk.core.filesystem.File,
- with_extension: bool = True,
Get the name of the File.
- Parameters:
with_extension (bool, optional) -- Whether to include the file extension. Defaults to True.
- Returns:
The file name, with or without extension.
- Return type:
Example
>>> from ostk.core.filesystem import Path >>> file = File.path(Path.parse("/path/to/document.pdf")) >>> file.get_name() # "document.pdf" >>> file.get_name(with_extension=False) # "document"
- get_parent_directory(
- self: ostk.core.filesystem.File,
Get the parent directory of the File.
- Returns:
The parent directory containing this file.
- Return type:
Example
>>> from ostk.core.filesystem import Path >>> file = File.path(Path.parse("/home/user/document.txt")) >>> parent = file.get_parent_directory() >>> str(parent.get_path()) # "/home/user"
- get_path(self: ostk.core.filesystem.File) ostk.core.filesystem.Path ¶
Get the full path of the File.
- Returns:
The complete path to the file.
- Return type:
Example
>>> from ostk.core.filesystem import Path >>> file = File.path(Path.parse("/home/user/document.txt")) >>> path = file.get_path() >>> str(path) # "/home/user/document.txt"
- get_permissions(
- self: ostk.core.filesystem.File,
Get the file permissions.
- Returns:
The permission set for the file.
- Return type:
Example
>>> from ostk.core.filesystem import Path >>> file = File.path(Path.parse("/etc/passwd")) >>> permissions = file.get_permissions()
- is_defined(self: ostk.core.filesystem.File) bool ¶
Check if the File is defined.
- Returns:
True if the File is defined, False otherwise.
- Return type:
Example
>>> from ostk.core.filesystem import Path >>> file = File.path(Path.parse("/path/to/file.txt")) >>> file.is_defined() # True >>> undefined_file = File.undefined() >>> undefined_file.is_defined() # False
- move_to_directory(
- self: ostk.core.filesystem.File,
- directory: ostk.core.filesystem.Directory,
Move the File to a different directory.
- Parameters:
directory (Directory) -- The target directory to move the file to.
Example
>>> from ostk.core.filesystem import Path >>> file = File.path(Path.parse("/tmp/document.txt")) >>> target_dir = Directory.path(Path.parse("/home/user/Documents")) >>> file.move_to_directory(target_dir)
- static path(path: ostk.core.filesystem.Path) ostk.core.filesystem.File ¶
Create a File from a file path.
- Parameters:
path (Path) -- The path to the file.
- Returns:
A File object representing the specified path.
- Return type:
Example
>>> from ostk.core.filesystem import Path >>> path = Path.parse("/home/user/document.txt") >>> file = File.path(path) >>> file.get_name() # "document.txt"
- remove(self: ostk.core.filesystem.File) None ¶
Remove the File from the filesystem.
Deletes the file if it exists.
- Raises:
RuntimeError -- If the file cannot be removed.
Example
>>> from ostk.core.filesystem import Path >>> file = File.path(Path.parse("/tmp/temp_file.txt")) >>> file.remove()
- to_string(self: ostk.core.filesystem.File) ostk.core.type.String ¶
Convert the File to a string representation.
- Returns:
String representation of the file path.
- Return type:
Example
>>> from ostk.core.filesystem import Path >>> file = File.path(Path.parse("/home/user/document.txt")) >>> file.to_string() # "/home/user/document.txt"
- static undefined() ostk.core.filesystem.File ¶
Create an undefined File.
- Returns:
An undefined File object.
- Return type:
Example
>>> undefined_file = File.undefined() >>> undefined_file.is_defined() # False