Directory#
- class nnodes.directory.Directory(cwd: str)#
Directory related operations.
- call(cmd: str)#
Call a shell command.
- Parameters:
cmd (str) – Shell command.
- async call_async(cmd: str)#
Call a shell command asynchronously.
- Parameters:
cmd (str) – Shell command.
- cp(src: str, dst: str = '.', *, mkdir: bool = True)#
Copy file or a directory.
- Parameters:
src (str) – Relative path to the file or directory to be copied.
dst (str, optional) – Relative path to the destination directory. Defaults to ‘.’.
mkdir (bool, optional) – Whether or not create a new directory if dst does not exist. Defaults to True.
- property cwd: str#
Path of the directory.
- Type:
str
- dump(obj, dst: str, ext: Optional[Literal['pickle', 'npy', 'toml', 'json', None]] = None, *, mkdir: bool = True)#
Dump a pickle / toml / json / npy file.
- Parameters:
obj (Any) – Object to be dumped.
dst (str) – Relative path to the file.
ext (DumpType, optional) – Type of the file to be dumped. Defaults to None.
- Raises:
TypeError – Unsupporte file type.
- has(src: str = '.') bool #
Check if a file or a directory exists.
- Parameters:
src (str, optional) – Relative path to the file or directory. Defaults to ‘.’.
- Returns:
Whether file exists.
- Return type:
bool
- isdir(src: str = '.') bool #
Check if src is a directory.
- Parameters:
src (str, optional) – Relative path to be checked. Defaults to ‘.’.
- Returns:
Whether src is a directory.
- Return type:
bool
- ln(src: str, dst: str = '.', mkdir: bool = True)#
Link a file or a directory.
- Parameters:
src (str) – Relative path to the file or directory to be linked.
dst (str, optional) – Relative path to the destination directory. Defaults to ‘.’.
mkdir (bool, optional) – Whether or not create a new directory if dst does not exist. Defaults to True.
- load(src: str, ext: Optional[Literal['pickle', 'npy', 'toml', 'json', None]] = None) Any #
Load a pickle / toml / json / npy file.
- Parameters:
src (str) – Relative path to the file.
ext (DumpType, optional) – Type of the file to be read. Defaults to None.
- Raises:
TypeError – Unsupporte file type.
- Returns:
Content of the file.
- Return type:
Any
- ls(src: str = '.', grep: str = '*', isdir: Optional[bool] = None) List[str] #
List items in a directory.
- Parameters:
src (str, optional) – Relative path to target directed. Defaults to ‘.’.
grep (str, optional) – Patten to filter listed items. Defaults to ‘*’.
isdir (bool | None, optional) – True: list directories only, False: list files only, None: list both files and directories. Defaults to None.
- Returns:
Items in the directory.
- Return type:
tp.List[str]
- mkdir(dst: str = '.')#
Create a new directory recursively.
- Parameters:
dst (str, optional) – Relative path to the directory to be created. Defaults to ‘.’.
- mv(src: str, dst: str = '.', *, mkdir: bool = True)#
Move a file or a directory.
- Parameters:
src (str) – Relative path to the file or directory to be moved.
dst (str, optional) – Relative path to the destination directory. Defaults to ‘.’.
mkdir (bool, optional) – Whether or not create a new directory if dst does not exist. Defaults to True.
- path(*paths: str, abs: bool = False) str #
Get relative or absolute path of current or a sub directory.
- Parameters:
*paths (str) – Paths to child directory.
abs (bool, optional) – Return absolute path instead. Defaults to False.
- Returns:
Path of target directory.
- Return type:
str
- read(src: str) str #
Read text file.
- Parameters:
src (str) – Path to the text file.
- Returns:
Content to the text file.
- Return type:
str
- readlines(src: str) List[str] #
Read lines of a text file.
- Parameters:
src (str) – Relative path to the text file.
- Returns:
Lines of the text file.
- Return type:
List[str]
- rel(src: str | nnodes.directory.Directory, *paths: str) str #
Convert from a path relative to root directory to a path relative to current directory.
- Parameters:
src (str | Directory) – Path or directory object.
- Returns:
Relative path from self to src.
- Return type:
str
- rm(src: str = '.')#
Remove a file or a directory.
- Parameters:
src (str, optional) – Relative path to the file or directory. Defaults to ‘.’.
- subdir(*paths: str) Directory #
Create a subdirectory object.
- Parameters:
*paths (str) – Relative paths to subdirectory.
- Returns:
Subdirectory object.
- Return type:
- write(text: str, dst: str, mode: str = 'w', *, mkdir: bool = True)#
Write a text file.
- Parameters:
text (str) – Content of the text file.
dst (str) – Relative path to the text file.
mode (str, optional) – Write mode. Defaults to ‘w’.
mkdir (bool, optional) – Creates a new directory if dst does not exist. Defaults to True.
- writelines(lines: Iterable[str], dst: str, mode: str = 'w', *, mkdir: bool = True)#
Write lines of a text file.
- Parameters:
lines (Iterable[str]) – Lines of the text file.
dst (str) – Relative path to the text file.
mode (str, optional) – Write mode. Defaults to ‘w’.
mkdir (bool, optional) – Creates a new directory if dst does not exist. Defaults to True.