fs
CompletedCommand ¶
CompletedCommand(args: str | list[str], returncode: int, time_taken: float, stdout: str | bytes | None = None, stderr: str | bytes | None = None)
Bases: CompletedProcess
Source code in stdl/fs.py
PathBase ¶
Bases: PathLike
Base class for typed filesystem paths.
Return-value policy:
- methods that keep the same lexical path return self
- methods that point at a different path return a new path object
- methods that create a second path return the created path object
Initialize a PathBase object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike
|
The path. |
required |
abs
|
bool
|
Whether to use the absolute path. |
False
|
Source code in stdl/fs.py
parents
property
¶
All parent directories from immediate parent to root.
resolve ¶
resolve(strict: bool = False) -> Self
Make the path absolute, resolving any symlinks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strict
|
bool
|
If True and path doesn't exist, raises FileNotFoundError. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
Self
|
The resolved path object. |
Source code in stdl/fs.py
rename ¶
rename(name: str) -> Self
chmod ¶
chmod(mode: int) -> Self
Change the path's permissions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
int
|
The new permissions mode. |
required |
chown ¶
chown_async
async
¶
should_exist ¶
should_exist_async
async
¶
should_not_exist ¶
should_not_exist_async
async
¶
stat ¶
stat(*, follow_symlinks: bool = True) -> stat_result
Return the stat info for this path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
follow_symlinks
|
bool
|
If True, follow symlinks. If False, return info about the link itself. |
True
|
Returns:
| Type | Description |
|---|---|
stat_result
|
os.stat_result: The stat information. |
Source code in stdl/fs.py
stat_async
async
¶
stat_async(*, follow_symlinks: bool = True) -> stat_result
samefile ¶
Return True if both paths reference the same filesystem entry.
samefile_async
async
¶
relative_to ¶
Make this path relative to another path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
str | PathBase
|
The base path to make this path relative to. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The relative path. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If this path is not relative to the other path. |
Source code in stdl/fs.py
expanduser ¶
Expand ~ and ~user constructs in the path.
Returns:
| Type | Description |
|---|---|
Self
|
The path object with the expanded path. |
as_uri ¶
as_uri() -> str
Return the path as a file URI.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The file URI (e.g., 'file:///path/to/file'). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the path is not absolute. |
Source code in stdl/fs.py
match ¶
remove_async
async
¶
delete_async
async
¶
move_to ¶
Move the path to a new target.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
PathBase
|
The destination path object. |
required |
mkdir
|
bool
|
Whether to create missing parent directories. Defaults to False. |
False
|
overwrite
|
bool
|
Whether to overwrite files if they already exist. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
Self
|
A new path object pointing at the moved path. |
Source code in stdl/fs.py
copy_to ¶
Copy the path to a new target.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
PathBase
|
The destination path object. |
required |
mkdir
|
bool
|
Whether to create missing parent directories. Defaults to False. |
False
|
overwrite
|
bool
|
Whether to overwrite files if they already exist. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
Self
|
A new path object pointing at the copied path. |
Source code in stdl/fs.py
get_xattr ¶
Retrieve the value of an extended attribute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the extended attribute. |
required |
group
|
str
|
The group of the extended attribute. Defaults to "user". |
'user'
|
Returns:
| Type | Description |
|---|---|
str
|
The value of the extended attribute. |
Source code in stdl/fs.py
get_xattr_async
async
¶
set_xattr ¶
Set an extended attribute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str | bytes
|
The value of the extended attribute. |
required |
name
|
str
|
The name of the extended attribute. |
required |
group
|
str
|
The group of the extended attribute. Defaults to "user". |
'user'
|
Source code in stdl/fs.py
set_xattr_async
async
¶
remove_xattr ¶
remove_xattr_async
async
¶
File ¶
Bases: PathBase
Initialize a File object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike
|
File path. |
required |
encoding
|
str
|
The file's encoding. |
'utf-8'
|
abs
|
bool
|
Whether to use the absolute path. |
False
|
Source code in stdl/fs.py
ext
property
¶
ext: str
The file's extension (without the dot). Returns empty string if the file has no extension.
suffix
property
¶
suffix: str
The file extension WITH the dot (e.g., '.txt'). Empty string if no extension.
size_readable
property
¶
size_readable: str
The file's size in a human-readable format if readable is set to True.
touch ¶
Create the file if needed and update its timestamps.
Source code in stdl/fs.py
touch_async
async
¶
read ¶
Read the contents of a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
Literal['r', 'rb']
|
The mode to open the file in. Use 'r' for text, 'rb' for binary. |
'r'
|
Returns:
| Type | Description |
|---|---|
str | bytes
|
str | bytes: The file contents as string (text mode) or bytes (binary mode). |
Source code in stdl/fs.py
read_async
async
¶
write ¶
Write data to a file, overwriting any existing data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
str | bytes
|
The data to write. |
required |
mode
|
Literal['w', 'wb']
|
The mode to open the file in. Use 'w' for text, 'wb' for binary. |
'w'
|
newline
|
bool
|
Whether to add a newline at the end. Ignored in binary mode. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
File |
File
|
The File object for method chaining. |
Source code in stdl/fs.py
append ¶
Append data to a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
str | bytes
|
The data to append. |
required |
mode
|
Literal['a', 'ab']
|
The mode to open the file in. Use 'a' for text, 'ab' for binary. |
'a'
|
newline
|
bool
|
Whether to add a newline at the end. Ignored in binary mode. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
File |
File
|
The File object for method chaining. |
Source code in stdl/fs.py
open ¶
Open the file and return a file handle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
str
|
The mode to open the file in. |
'r'
|
encoding
|
str | None
|
The encoding to use. Defaults to self.encoding for text modes. |
None
|
**kwargs
|
OpenKwarg
|
Additional arguments passed to the built-in open(). |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
IO |
IO[Any]
|
A file handle. |
Source code in stdl/fs.py
open_async
async
¶
open_async(mode: Literal['r'] = 'r', encoding: str | None = None, **kwargs: OpenKwarg) -> AsyncFileHandle[str]
open_async(mode: str = 'r', encoding: str | None = None, **kwargs: OpenKwarg) -> AsyncFileHandle[str] | AsyncFileHandle[bytes]
Async version of open().
Source code in stdl/fs.py
write_iter ¶
Write data from an iterable to a file, overwriting any existing data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Iterable
|
The data to write. |
required |
sep
|
str
|
The separator to use between items. |
'\n'
|
Returns:
| Name | Type | Description |
|---|---|---|
File |
File
|
The File object for method chaining. |
Source code in stdl/fs.py
write_iter_async
async
¶
append_iter ¶
Append data from an iterable to a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Iterable
|
The data to append. |
required |
sep
|
str
|
The separator to use between items. |
'\n'
|
Returns:
| Name | Type | Description |
|---|---|---|
File |
File
|
The File object for method chaining. |
Source code in stdl/fs.py
append_iter_async
async
¶
readlines ¶
readlines_async
async
¶
splitlines ¶
splitlines_async
async
¶
move_to ¶
Move the file to a new target.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
File | Directory
|
The destination |
required |
mkdir
|
bool
|
Whether to create missing parent directories. Defaults to False. |
False
|
overwrite
|
bool
|
Whether to overwrite the destination if it already exists. Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
File |
File
|
A new File object pointing at the moved path. |
Source code in stdl/fs.py
move_to_async
async
¶
Async version of move_to().
Source code in stdl/fs.py
copy_to ¶
Copy the file to a new target.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
File | Directory
|
The destination |
required |
mkdir
|
bool
|
Whether to create missing parent directories. Defaults to False. |
False
|
overwrite
|
bool
|
Whether to overwrite the destination if it already exists. Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
File |
File
|
A new File object pointing at the copied path. |
Source code in stdl/fs.py
copy_to_async
async
¶
Async version of copy_to().
Source code in stdl/fs.py
with_dir ¶
Change the directory of the file object. This will not move the actual file to that directory. Use File.move_to for that.
with_ext ¶
Change the extension of the file and return the new File object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ext
|
str
|
The new extension of the file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
File |
File
|
The File object with the new extension. |
Source code in stdl/fs.py
with_suffix ¶
Add a suffix to the file's name and return the new File object.
with_prefix ¶
Add a prefix to the file's name and return the new File object.
with_stem ¶
Return a new File with the stem changed, keeping the suffix.
rename ¶
Rename the file and return the new File object.
link ¶
Create a hard link and return a File for the created link path.
Source code in stdl/fs.py
link_async
async
¶
symlink ¶
Create a symbolic link and return a File for the created link path.
symlink_async
async
¶
rand
classmethod
¶
Create a new random file with a specified prefix and extension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
str
|
The prefix for the file name. |
'file'
|
ext
|
str
|
The extension for the file name. |
''
|
Returns:
| Name | Type | Description |
|---|---|---|
File |
File
|
A new File object with a random name. |
Source code in stdl/fs.py
Directory ¶
Bases: PathBase
Initialize a Directory object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike
|
Directory path. |
required |
abs
|
bool
|
Whether to use the absolute path. |
False
|
Source code in stdl/fs.py
size_readable
property
¶
size_readable: str
Total size of the directory and all its contents in human-readable form.
create ¶
create_async
async
¶
clear ¶
clear() -> Directory
Remove all children while keeping the directory itself.
Source code in stdl/fs.py
move_to ¶
Move this directory into another directory, preserving its basename.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
Directory
|
The destination parent directory. |
required |
mkdir
|
bool
|
Whether to create missing parent directories. Defaults to False. |
False
|
overwrite
|
bool
|
Whether to overwrite if target path already exists. Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
Directory |
Directory
|
A new Directory object pointing at the moved path. |
Source code in stdl/fs.py
move_to_async
async
¶
Async version of move_to().
Source code in stdl/fs.py
copy_to ¶
Copy this directory into another directory, preserving its basename.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
Directory
|
The destination parent directory. |
required |
mkdir
|
bool
|
Whether to create missing parent directories. Defaults to False. |
False
|
overwrite
|
bool
|
Whether to overwrite if target path already exists. Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
Directory |
Directory
|
A new Directory object pointing at the copied path. |
Source code in stdl/fs.py
copy_to_async
async
¶
Async version of copy_to().
Source code in stdl/fs.py
yield_files ¶
yield_files(ext: str | tuple[str, ...] | None = None, glob: str | Pattern[str] | None = None, regex: str | Pattern[str] | None = None, recursive: bool = True) -> Generator[File, None, None]
Yield files in the directory.
All filter parameters use AND logic - files must match all specified filters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ext
|
str | tuple[str, ...] | None
|
If provided, only yield files with provided extensions. |
None
|
glob
|
str | Pattern[str] | None
|
Glob pattern as string or compiled regex from fnmatch.translate(). |
None
|
regex
|
str | Pattern[str] | None
|
Regex pattern as string or compiled re.Pattern. |
None
|
recursive
|
bool
|
Whether to search recursively. |
True
|
Yields:
| Type | Description |
|---|---|
File
|
Generator[File, None, None]: The files in the directory. |
Source code in stdl/fs.py
yield_files_async
async
¶
yield_files_async(ext: str | tuple[str, ...] | None = None, glob: str | Pattern[str] | None = None, regex: str | Pattern[str] | None = None, recursive: bool = True) -> AsyncGenerator[File, None]
Async version of yield_files().
Source code in stdl/fs.py
get_files ¶
get_files(ext: str | tuple[str, ...] | None = None, glob: str | Pattern[str] | None = None, regex: str | Pattern[str] | None = None, recursive: bool = True) -> list[File]
Get files in the directory.
All filter parameters use AND logic - files must match all specified filters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ext
|
str | tuple[str, ...] | None
|
If provided, only yield files with provided extensions. |
None
|
glob
|
str | Pattern[str] | None
|
Glob pattern as string or compiled regex from fnmatch.translate(). |
None
|
regex
|
str | Pattern[str] | None
|
Regex pattern as string or compiled re.Pattern. |
None
|
recursive
|
bool
|
Whether to search recursively. |
True
|
Returns:
| Type | Description |
|---|---|
list[File]
|
list[File]: The files in the directory. |
Source code in stdl/fs.py
get_files_async
async
¶
get_files_async(ext: str | tuple[str, ...] | None = None, glob: str | Pattern[str] | None = None, regex: str | Pattern[str] | None = None, recursive: bool = True) -> list[File]
Async version of get_files().
Source code in stdl/fs.py
read_files_async
async
¶
read_files_async(*, mode: Literal['r', 'rb'] = 'r', ext: str | tuple[str, ...] | None = None, glob: str | Pattern[str] | None = None, regex: str | Pattern[str] | None = None, recursive: bool = True, concurrency: int = 8) -> list[tuple[File, str]] | list[tuple[File, bytes]]
Read matching files concurrently.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
Literal['r', 'rb']
|
The mode to read files in. Use "r" for text and "rb" for binary. |
'r'
|
ext
|
str | tuple[str, ...] | None
|
If provided, only read files with provided extensions. |
None
|
glob
|
str | Pattern[str] | None
|
Glob pattern as string or compiled regex from fnmatch.translate(). |
None
|
regex
|
str | Pattern[str] | None
|
Regex pattern as string or compiled re.Pattern. |
None
|
recursive
|
bool
|
Whether to search recursively. |
True
|
concurrency
|
int
|
Maximum number of concurrent read operations. |
8
|
Returns:
| Type | Description |
|---|---|
list[tuple[File, str]] | list[tuple[File, bytes]]
|
list[tuple[File, str]] | list[tuple[File, bytes]]: Matching files and their contents. |
Source code in stdl/fs.py
remove_files_async
async
¶
remove_files_async(*, ext: str | tuple[str, ...] | None = None, glob: str | Pattern[str] | None = None, regex: str | Pattern[str] | None = None, recursive: bool = True, concurrency: int = 8) -> list[File]
Remove matching files concurrently.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ext
|
str | tuple[str, ...] | None
|
If provided, only remove files with provided extensions. |
None
|
glob
|
str | Pattern[str] | None
|
Glob pattern as string or compiled regex from fnmatch.translate(). |
None
|
regex
|
str | Pattern[str] | None
|
Regex pattern as string or compiled re.Pattern. |
None
|
recursive
|
bool
|
Whether to search recursively. |
True
|
concurrency
|
int
|
Maximum number of concurrent remove operations. |
8
|
Returns:
| Type | Description |
|---|---|
list[File]
|
list[File]: The removed files in snapshot order. |
Source code in stdl/fs.py
yield_subdirs ¶
yield_subdirs(glob: str | Pattern[str] | None = None, regex: str | Pattern[str] | None = None, recursive: bool = True) -> Generator[Directory, None, None]
Yield subdirectories in the directory.
All filter parameters use AND logic - directories must match all specified filters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
glob
|
str | Pattern[str] | None
|
Glob pattern as string or compiled regex from fnmatch.translate(). |
None
|
regex
|
str | Pattern[str] | None
|
Regex pattern as string or compiled re.Pattern. |
None
|
recursive
|
bool
|
Whether to search recursively. |
True
|
Yields:
| Type | Description |
|---|---|
Directory
|
Generator[Directory, None, None]: The subdirectories in the directory. |
Source code in stdl/fs.py
yield_subdirs_async
async
¶
yield_subdirs_async(glob: str | Pattern[str] | None = None, regex: str | Pattern[str] | None = None, recursive: bool = True) -> AsyncGenerator[Directory, None]
Async version of yield_subdirs().
Source code in stdl/fs.py
get_subdirs ¶
get_subdirs(glob: str | Pattern[str] | None = None, regex: str | Pattern[str] | None = None, recursive: bool = True) -> list[Directory]
Get subdirectories in the directory.
All filter parameters use AND logic - directories must match all specified filters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
glob
|
str | Pattern[str] | None
|
Glob pattern as string or compiled regex from fnmatch.translate(). |
None
|
regex
|
str | Pattern[str] | None
|
Regex pattern as string or compiled re.Pattern. |
None
|
recursive
|
bool
|
Whether to search recursively. |
True
|
Returns:
| Type | Description |
|---|---|
list[Directory]
|
list[Directory]: The subdirectories in the directory. |
Source code in stdl/fs.py
get_subdirs_async
async
¶
get_subdirs_async(glob: str | Pattern[str] | None = None, regex: str | Pattern[str] | None = None, recursive: bool = True) -> list[Directory]
Async version of get_subdirs().
Source code in stdl/fs.py
yield_entries ¶
Yield immediate child files and directories in a single pass.
Source code in stdl/fs.py
yield_entries_async
async
¶
yield_entries_async(*, abs: bool = False) -> AsyncGenerator[File | Directory, None]
Async version of yield_entries().
Source code in stdl/fs.py
get_entries ¶
get_entries_async
async
¶
walk ¶
walk(*, follow_symlinks: bool = False, abs: bool = False) -> Generator[tuple[Directory, list[Directory], list[File]], None, None]
Recursively walk the directory tree in top-down order.
Yields:
| Type | Description |
|---|---|
Directory
|
tuple[Directory, list[Directory], list[File]]: The current root, its child directories, |
list[Directory]
|
and its child files. |
Source code in stdl/fs.py
walk_async
async
¶
walk_async(*, follow_symlinks: bool = False, abs: bool = False) -> AsyncGenerator[tuple[Directory, list[Directory], list[File]], None]
Async version of walk().
Source code in stdl/fs.py
pickle_load ¶
pickle_load_async
async
¶
pickle_dump ¶
pickle_dump_async
async
¶
json_load ¶
Load a JSON file from the given path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike
|
The path of the JSON file to load. |
required |
encoding
|
str
|
The encoding of the file. Defaults to "utf-8". |
'utf-8'
|
Returns:
| Type | Description |
|---|---|
dict[Any, Any] | list[dict[Any, Any]]
|
dict | list[dict]: The JSON data loaded from the file. |
Source code in stdl/fs.py
json_load_async
async
¶
json_append ¶
json_append(data: JsonValue, filepath: str | PathLike, encoding: str = 'utf-8', default: Callable[[Any], str] = str, indent: int = 4) -> None
Append data to a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict | list[dict]
|
The data to be appended |
required |
filepath
|
str | PathLike
|
The path of the JSON file |
required |
encoding
|
str
|
The encoding of the file. Defaults to "utf-8". |
'utf-8'
|
default
|
A function that gets called for objects that can't otherwise be serialized. See json.dump() documentation for more information. |
required | |
indent
|
int
|
The number of spaces to use for indentation. Defaults to 4. |
4
|
Source code in stdl/fs.py
json_append_async
async
¶
json_append_async(data: JsonValue, filepath: str | PathLike, encoding: str = 'utf-8', default: Callable[[Any], str] = str, indent: int = 4) -> None
Async version of json_append().
Source code in stdl/fs.py
json_dump ¶
json_dump(data: JsonValue, path: str | PathLike, encoding: str = 'utf-8', default: Callable[[Any], str] = str, indent: int = 4) -> None
Dumps data to a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
JsonValue
|
data to be dumped |
required |
path
|
str | PathLike
|
path to the output file |
required |
encoding
|
str
|
encoding of the output file. Default: 'utf-8' |
'utf-8'
|
default
|
Callable[[Any], str]
|
A function that gets called on objects that cannot be serialized. Default: str |
str
|
indent
|
int
|
number of spaces to use when indenting the output json. Default: 4 |
4
|
Source code in stdl/fs.py
json_dump_async
async
¶
json_dump_async(data: JsonValue, path: str | PathLike, encoding: str = 'utf-8', default: Callable[[Any], str] = str, indent: int = 4) -> None
Async version of json_dump().
Source code in stdl/fs.py
yaml_load ¶
Load a YAML file from the given path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike
|
The path of the YAML file to load. |
required |
encoding
|
str
|
The encoding of the file. Defaults to "utf-8". |
'utf-8'
|
Returns:
| Type | Description |
|---|---|
dict[Any, Any] | list[dict[Any, Any]]
|
dict | list[dict]: The YAML data loaded from the file. |
Source code in stdl/fs.py
yaml_load_async
async
¶
yaml_dump ¶
Dumps data to a YAML file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
YamlValue
|
data to be dumped |
required |
path
|
Pathlike
|
path to the output file |
required |
encoding
|
str
|
encoding of the output file. Default: 'utf-8' |
'utf-8'
|
Source code in stdl/fs.py
yaml_dump_async
async
¶
toml_load_async
async
¶
toml_dump_async
async
¶
get_dir_size ¶
Calculates total size of a directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
directory
|
(str, Path)
|
target directory |
required |
readable
|
bool
|
Return the size in human-readable format |
False
|
Source code in stdl/fs.py
move_files ¶
Moves files to a specified directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
files
|
list[str | PathLike]
|
List of files to be moved |
required |
directory
|
str | PathLike
|
target directory |
required |
mkdir
|
bool
|
whether to create the directory if it doesn't exist. Default: False |
False
|
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
if the target directory does not exist and mkdir is False. |
Source code in stdl/fs.py
rand_filename ¶
Generates a random filename with the given prefix and extension. Optionally includes current date and time in the filename.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
str
|
Filename prefix. |
'file'
|
ext
|
str
|
Filename extension. |
''
|
include_datetime
|
bool
|
Whether to include date and time. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The generated random filename. |
Source code in stdl/fs.py
bytes_readable ¶
Convert bytes to a human-readable string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size_bytes
|
int
|
The number of bytes |
required |
Returns: str : The number of bytes in a human-readable format
Source code in stdl/fs.py
readable_size_to_bytes ¶
Convert human-readable string to bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
str
|
The number of bytes in human-readable format |
required |
kb_size
|
int
|
The byte size of a kilobyte (1000 or 1024). Defaults to 1024. |
1024
|
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The number of bytes |
Source code in stdl/fs.py
windows_has_drive ¶
Check if a drive letter exists on Windows. Will always return False if the platform is not Windows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
letter
|
str
|
The letter of the drive. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
Whether the drive exists. |
Source code in stdl/fs.py
mkdir ¶
Creates a directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
The path of the directory to create. |
required |
mode
|
int
|
The mode to set for the directory. Defaults to 511 (octal 0777). |
511
|
exist_ok
|
bool
|
Whether to raise an exception if the directory already exists. Defaults to True. |
True
|
Source code in stdl/fs.py
mkdirs ¶
Creates directories inside a destination directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dest
|
str | Path
|
The destination directory. |
required |
names
|
list[str]
|
A list of directory names to be created in the destination directory. |
required |
Source code in stdl/fs.py
yield_files_in ¶
yield_files_in(directory: str | PathLike, ext: str | tuple[str, ...] | None = None, *, recursive: bool = True, abs: bool = True) -> Generator[str, None, None]
Yields the paths of files in a directory.
This function searches for files in a directory and yields their paths.
If the ext parameter is provided, only files with that extension are yielded. The ext parameter is case-insensitive.
If the recursive parameter is set to True, the function will search for files in subdirectories recursively.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
directory
|
str | Path
|
The directory to search. |
required |
ext
|
str | tuple[str, ...]
|
If provided, only yield files with provided extensions. |
None
|
recursive
|
bool
|
Whether to search recursively. |
True
|
abs
|
bool
|
Whether to convert paths to absolute paths. |
True
|
Yields:
| Type | Description |
|---|---|
str
|
Generator[str, None, None]: The absolute paths of the files in the directory, matching the provided extension. |
Source code in stdl/fs.py
get_files_in ¶
get_files_in(directory: str | PathLike, ext: str | tuple[str, ...] | None = None, *, recursive: bool = True, abs: bool = True) -> list[str]
Returns the paths of files in a directory.
This function searches for files in a directory and yields their paths.
If the ext parameter is provided, only files with that extension are returned. The ext parameter is case-insensitive.
If the recursive parameter is set to True, the function will search for files in subdirectories recursively.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
directory
|
str | Path
|
The directory to search. |
required |
ext
|
str | tuple[str, ...]
|
If provided, only yield files with provided extensions. Defaults to None. |
None
|
recursive
|
bool
|
Whether to search recursively. Defaults to True. |
True
|
abs
|
bool
|
Whether to convert paths to absolute paths. |
True
|
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: The absolute path of the files in the directory, matching the provided extension. |
Source code in stdl/fs.py
yield_dirs_in ¶
yield_dirs_in(directory: str | PathLike, *, recursive: bool = True, abs: bool = True) -> Generator[str, None, None]
Yields paths to all directories in the specified directory. Yielded paths are converted to absolute paths.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
directory
|
str | Path
|
The directory to search. |
required |
recursive
|
bool
|
Whether to search recursively. |
True
|
abs
|
bool
|
Whether to convert paths to absolute paths. |
True
|
Yields:
| Type | Description |
|---|---|
str
|
Generator[str, None, None]: The paths of the directories that are found during travelsal. |
Source code in stdl/fs.py
get_dirs_in ¶
Returns all directories in the specified directory. Returned paths are converted to absolute paths.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
directory
|
str | Path
|
The directory to search. |
required |
recursive
|
bool
|
Whether to search recursively. |
True
|
abs
|
bool
|
Whether to convert paths to absolute paths. |
True
|
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: The paths of the directories that are found during travelsal. |
Source code in stdl/fs.py
ensure_paths_exist ¶
Ensures that the specified paths exist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
one or more strings representing the paths to check. Can also include an Iterable of paths. |
required |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
if one of the provided paths does not exist. |
Source code in stdl/fs.py
ensure_paths_dont_exist ¶
Ensures that the specified paths don't exist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
one or more strings representing the paths to check. Can also include an Iterable of paths. |
required |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
if one of the provided paths exists. |
Source code in stdl/fs.py
exec_cmd ¶
exec_cmd(cmd: list[str] | str, timeout: float | None = None, shell: bool = False, capture_output: bool = True, check: bool = False, cwd: str | None = None, stdin: IO | None = None, stdout: IO | None = None, stderr: IO | None = None, input: str | None | bytes = None, env: dict[str, str] | None = None, text: bool = True, *args: SubprocessRunArg, **kwargs: SubprocessRunKwarg) -> CompletedCommand
Wrapper for subprocess.run with nicer default arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cmd
|
str | list[str]
|
command to run. A list of strings or a single string. |
required |
timeout
|
float
|
the time after which the command is killed. |
None
|
shell
|
bool
|
whether or not to run the command in a shell. |
False
|
capture_output
|
bool
|
whether or not to capture the output to stdout and stderr. |
True
|
check
|
bool
|
whether or not to raise an exception on a non-zero exit code. |
False
|
cwd
|
str
|
current working directory to run the command in. |
None
|
stdin
|
IO
|
file object to read stdin from. |
None
|
stdout
|
IO
|
file object to write stdout to. |
None
|
stderr
|
IO
|
file object to write stderr to. |
None
|
input
|
str | bytes
|
input to send to the command. |
None
|
env
|
dict
|
environment variables to pass to the new process. |
None
|
text
|
bool
|
whether or not to return output as text or bytes. |
True
|
*args
|
additional arguments to pass to subprocess.run. |
required | |
**kwargs
|
additional keyword arguments to pass to subprocess.run. |
required |
Returns:
| Type | Description |
|---|---|
CompletedCommand
|
subprocess.CompletedProcess : the completed process. |
Source code in stdl/fs.py
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 | |
start_file ¶
Open the file with your OS's default application.
This function determines the current operating system and uses the appropriate command to open the specified file with the default application. It supports Windows, macOS, and Linux, including Windows Subsystem for Linux (WSL).