Source code for jammy.io.common
import os.path as osp
import math
import contextlib
__all__ = ["get_ext", "fsize_format", "auto_close", "get_name"]
unit_list = list(zip(["bytes", "kB", "MB", "GB", "TB", "PB"], [0, 0, 1, 2, 2, 2]))
[docs]def get_ext(fname, match_first=False):
if match_first:
fname = osp.split(fname)[1]
return fname[fname.find(".") :]
else:
return osp.splitext(fname)[1]
[docs]def get_name(fname, match_first=False):
ext = get_ext(fname, match_first)
return fname[:-len(ext)]
[docs]@contextlib.contextmanager
def auto_close(file):
yield file
file.close()