diff --git a/monty/shutil.py b/monty/shutil.py index a68cba847..ef88db51c 100644 --- a/monty/shutil.py +++ b/monty/shutil.py @@ -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") @@ -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) @@ -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: @@ -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)