fs
File ¶
Bases: PathLike
Initialize a File object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path |
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.
size ¶
The file's size in bytes or a human-readable format if readable is set to True.
create ¶
remove ¶
clear ¶
write ¶
write(data, *, newline: bool = True) -> None
Write data to a file, overwriting any existing data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data |
Any
|
The data to write. |
required |
newline |
bool
|
Whether to add a newline at the end of the data. |
True
|
Source code in stdl/fs.py
append ¶
append(data, *, newline: bool = True)
Append data to a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data |
Any
|
The data to append. |
required |
newline |
bool
|
Whether to add a newline at the end of the data. |
True
|
write_iter ¶
write_iter(data: Iterable, sep='\n')
append_iter ¶
append_iter(data: Iterable, sep='\n')
readlines ¶
splitlines ¶
move_to ¶
move_to(directory: str, *, overwrite=True)
Move the file to a new directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
directory |
str
|
The destination directory. |
required |
overwrite |
bool
|
Whether to overwrite the file if it already exists in the destination directory. Defaults to True. |
True
|
Source code in stdl/fs.py
copy_to ¶
copy_to(directory: str, *, mkdir=False, overwrite=True)
Copy the file to a new directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
directory |
str
|
The destination directory. |
required |
overwrite |
bool
|
Whether to overwrite the file if it already exists in the destination directory. Defaults to True. |
True
|
Source code in stdl/fs.py
with_dir ¶
with_dir(directory: str)
Change the directory of the file object. This will not move the actual file to that directory. Use File.move_to for that.
Source code in stdl/fs.py
with_ext ¶
with_ext(ext: str)
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 |
The File object with the new extension. |
Source code in stdl/fs.py
with_suffix ¶
with_suffix(suffix: str)
Add a suffix to the file's name and return the new File object.
with_prefix ¶
with_prefix(prefix: str)
Add a prefix to the file's name and return the new File object.
chown ¶
should_exist ¶
should_not_exist ¶
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 |
A new File object with a random name. |
Source code in stdl/fs.py
get_xattr ¶
Retrieve the value of an extended attribute for the file.
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:
| Name | Type | Description |
|---|---|---|
str |
str
|
The value of the extended attribute. |
Source code in stdl/fs.py
set_xattr ¶
Set an extended attribute for the file.
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
CompletedCommand ¶
CompletedCommand(args, returncode: int, time_taken: float, stdout: Any | None = None, stderr: Any | None = None)
Bases: CompletedProcess
Source code in stdl/fs.py
pickle_load ¶
pickle_dump ¶
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 | list[dict]
|
dict | list[dict]: The JSON data loaded from the file. |
Source code in stdl/fs.py
json_append ¶
json_append(data: dict | list[dict], filepath: str | PathLike, encoding: str = 'utf-8', default=str, indent: int = 4)
Append data to a JSON file. Args: data (dict | list[dict]): The data to be appended filepath (str | PathLike): The path of the JSON file encoding (str, optional): The encoding of the file. Defaults to "utf-8". default : A function that gets called for objects that can’t otherwise be serialized. See json.dump() documentation for more information. indent (int, optional): The number of spaces to use for indentation. Defaults to 4.
Source code in stdl/fs.py
json_dump ¶
Dumps data to a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data |
Any
|
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 |
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
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 | list[dict]
|
dict | list[dict]: The YAML data loaded from the file. |
Source code in stdl/fs.py
yaml_dump ¶
Dumps data to a YAML file Args: data: data to be dumped path (Pathlike): path to the output file encoding (str): encoding of the output file. Default: 'utf-8'
Source code in stdl/fs.py
get_dir_size ¶
Moves files to a specified 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. Args: size_bytes (int): The number of bytes 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. Args: size (str): The number of bytes in human-readable format kb_size (int, optional): The byte size of a kilobyte (1000 or 1024). Defaults to 1024. Returns: 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. Args: letter (str): The letter of the drive. Returns: 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 |
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 | Path, ext: str | tuple | 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 | Path, ext: str | tuple | 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 | Path, *, 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. |
()
|
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. |
()
|
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, shell: bool = False, capture_output: bool = True, check: bool = False, cwd: str = None, stdin: IO = None, stdout: IO = None, stderr: IO = None, input: str | bytes = None, env: dict = None, text: bool = True, *args, **kwargs) -> 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. |
()
|
|
**kwargs |
additional keyword arguments to pass to subprocess.run. |
{}
|
Returns:
| Type | Description |
|---|---|
CompletedCommand
|
subprocess.CompletedProcess : the completed process. |
Source code in stdl/fs.py
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).