Skip to content

Commit

Permalink
Port os to pathlib
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew-S-Rosen committed Sep 4, 2023
1 parent f34f4fc commit a88461c
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions monty/shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def gzip_dir(path: str | Path, compresslevel: int = 6) -> None:
for root, _, files in os.walk(path):
for f in files:
full_f = os.path.abspath(os.path.join(root, f))
if not f.lower().endswith("gz") and not os.path.isdir(full_f):
if not f.suffix.lower() == ".gz" and not full_f.is_dir():
with open(full_f, "rb") as f_in, GzipFile(f"{full_f}.gz", "wb", compresslevel=compresslevel) as f_out:
shutil.copyfileobj(f_in, f_out)
shutil.copystat(full_f, f"{full_f}.gz")
Expand All @@ -76,7 +76,7 @@ def compress_file(filepath: str | Path, compression="gz") -> None:
filepath = Path(filepath)
if compression not in ["gz", "bz2"]:
raise ValueError("Supported compression formats are 'gz' and 'bz2'.")
if not filepath.lower().endswith(f".{compression}"):
if not filepath.suffix.lower() in (f".{compression}"):
with open(filepath, "rb") as f_in, zopen(f"{filepath}.{compression}", "wb") as f_out:
f_out.writelines(f_in)
os.remove(filepath)
Expand All @@ -96,7 +96,7 @@ def compress_dir(path: str | Path, compression="gz") -> None:
path = Path(path)
for parent, _, files in os.walk(path):
for f in files:
compress_file(os.path.join(parent, f), compression=compression)
compress_file(Path(parent, f), compression=compression)


def decompress_file(filepath: str | Path) -> str | None:
Expand Down Expand Up @@ -155,6 +155,6 @@ def remove(path: str | Path, follow_symlink=False) -> None:
elif path.is_symlink():
if follow_symlink:
remove(os.readlink(path))
os.unlink(path)
Path.unlink(path)
else:
shutil.rmtree(path)

0 comments on commit a88461c

Please sign in to comment.