Skip to content

Commit

Permalink
Add SPath.move_dir
Browse files Browse the repository at this point in the history
  • Loading branch information
Setsugennoao committed Aug 6, 2024
1 parent 6bac1fe commit 38197a5
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion stgpytools/types/file.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from os import PathLike, path
from os import PathLike, listdir, path
from pathlib import Path
from typing import TYPE_CHECKING, Any, Callable, Iterable, Literal, TypeAlias, Union

Expand Down Expand Up @@ -120,5 +120,22 @@ def append_to_stem(self, suffixes: str | Iterable[str], sep: str = '_') -> SPath

return self.with_stem(sep.join([self.stem, *to_arr(suffixes)])) # type:ignore[list-item]

def move_dir(self, dst: SPath, *, mode: int = 0o777) -> None:
dst.mkdir(mode, True, True)

for file in listdir(self):
src_file = self / file
dst_file = dst / file

print(file)
print('moving', src_file, 'into', dst_file)

if dst_file.exists():
src_file.unlink()
else:
src_file.rename(dst_file)

self.rmdir()


SPathLike = Union[str, Path, SPath]

0 comments on commit 38197a5

Please sign in to comment.