The cause of

OS/OS. Some of the path/shutil interfaces, often make me confused, for a while in the library, for a while in the library, sometimes there are several interface seems to be the same function, such as copy/used by copyfile/copyfileobj; The remove file interface appears in OS, and the Remove dir interface is in Shutil, which is often confusing.

In addition, the older you get, the more you remember things, the less you remember them, and it’s no use memorizing over and over again, which makes you sad.

Java seems to have no such tangle, the interface is very intuitive, almost no MEMORY of the API, every time the IDE auto-complete (use fewer interfaces, according to the IDE popover can also be selected temporarily, do not need to go into source code or documentation).

There is a natural incentive to simply wrap up these miscellaneous interfaces, and this article aims to reduce this mental burden and provide as consistent an API as possible.

packaging

The library documentation is easy to understand, not much to discuss, and the Java API for the corresponding class makes packaging a breeze.

As follows:

import os
import shutil


class Path:
    def __init__(self, pathstr: str):
        self._pathstr = pathstr
        self._abspath = os.path.abspath(pathstr)

    def get_parent(self):
        return Path(os.path.dirname(self._abspath))

    def get_childs(self):
        return [Path(c)
                for c in os.listdir(self._abspath)]

    def is_absolute(self):
        return os.path.isabs(self._pathstr)

    def is_dir(self):
        return os.path.isdir(self._abspath)

    def is_file(self):
        return os.path.isfile(self._abspath)

    def join(self, s):
        return Path(os.path.join(self._abspath, s))

    def get_basename(self):
        return os.path.basename(self._abspath)

    def get_extension(self):
        return os.path.splitext(self._abspath)[1]

    def get_filename(self):
        return os.path.split(self._abspath)[1]

    def is_exists(self):
        return os.path.exists(self._abspath)

    def ends_with(self, ext):
        return self._abspath.endswith(ext)

    def touch(self):
        if self.is_exists():
            return True

        open(self._abspath, mode='r', encoding='utf-8').close()

        return True

    def mkdir(self):
        if self.is_exists():
            return

        os.makedirs(self._abspath)

        return True

    def rm(self):
        if not self.is_exists():
            return

        if self.is_file():
            os.remove(self._abspath)
        if self.is_dir():
            shutil.rmtree(self._abspath)

    def mv(self, dest):
        shutil.move(self._abspath, dest)

    def cp(self, dest):
        if self.is_file():
            shutil.copyfile(self._abspath, dest)

        if self.is_dir():
            shutil.copytree(self._abspath, dest)


class File:
    def __init__(self, filestr: str):
        if not os.path.isfile(filestr):
            raise ValueError('must be a real file, now is %s' % filestr)

        self._filestr = os.path.abspath(filestr)
        self._statresult = os.stat(self._filestr)

    def get_size(self):
        return self._statresult.st_size

    def get_create_time(self):
        return self._statresult.st_ctime
Copy the code

Use as follows:

Path('/root/newfile').touch()
Path('/root/newdir').mkdir()
Copy the code

Put it under the utils/ of your project so you don’t have to remember all those messy apis anymore. 🙂

benefits

In fact, there are many apis in the Python standard library that require occasional documentation or memorization, and that require frequent documentation. At this point, a simple layer of packaging or forwarding can immediately reduce the pain