mate_mutate_methods#

Provide methods to mutate the Path instance.

class pathlib_mate.mate_mutate_methods.MutateMethods[source]#

Provide methods to mutate the Path instance.

drop_parts(n=1)[source]#

Drop number of parts from the ends. By default, it is equal to self.parent.

Example:

>>> Path("/usr/bin/python").drop_parts(1)
"/user/bin"

>>> Path("/usr/bin/python").drop_parts(2)
"/user"
Parameters:

n (int) – integer, number of parts you wants to drop from ends. n has to greater equal than 0.

Return type:

Path

Returns:

a new Path object.

append_parts(*parts)[source]#

Append some parts to the end of this path.

Example:

>>> Path("/usr/bin/python").append_parts("lib")
"/user/bin/python/lib"

>>> Path("/usr/bin/python").append_parts("lib", "core.py")
"/user/bin/python/lib/core.py"
Return type:

Path

Returns:

a new Path object.

change(new_abspath=None, new_dirpath=None, new_dirname=None, new_basename=None, new_fname=None, new_ext=None)[source]#

Return a new pathlib_mate.pathlib2.Path object with updated path.

Example:

>>> Path("/Users/alice/test.py").change(new_fname="test1")
/Users/alice/test1.py

>>> Path("/Users/alice/test.py").change(new_ext=".txt")
/Users/alice/test.txt

>>> Path("/Users/alice/test.py").change(new_dirname="bob")
/Users/bob/test.py

>>> Path("/Users/alice/test.py").change(new_dirpath="/tmp")
/tmp/test.py
Return type:

Path

中文文档

高级重命名函数, 允许用于根据路径的各个组成部分进行重命名. 但和 os.rename 方法一样, 需要保证母文件夹存在.

is_not_exist_or_allow_overwrite(overwrite=False)[source]#

Test whether a file target is not exists or it exists but allow overwrite.

Parameters:

overwrite – bool

Return type:

bool

moveto(new_abspath=None, new_dirpath=None, new_dirname=None, new_basename=None, new_fname=None, new_ext=None, overwrite=False, makedirs=False)[source]#

Similar to change() method. However, it move the original path to new location.

Return type:

Path

中文文档

高级 文件 / 文件夹 移动函数, 允许用于根据路径的各个组成部分进行重命名, 然后移动.

copyto(new_abspath=None, new_dirpath=None, new_dirname=None, new_basename=None, new_fname=None, new_ext=None, overwrite=False, makedirs=False)[source]#

Similar to change() method. However, it copy the original path to new location.

Return type:

Path

中文文档

高级 文件 / 文件夹 拷贝函数, 允许用于根据路径的各个组成部分进行重命名, 然后拷贝.

remove()[source]#

Remove this file. Won’t work if it is a directory.

remove_if_exists()[source]#

Remove a file or entire directory recursively.

mkdir_if_not_exists()[source]#

Make a directory if not exists yet.

classmethod dir_here(file_var)[source]#

Return the directory of the python script that where this method is called.

Suppose you have a file structure like this:

/Users/myname/test.py

And it is the content of test.py:

from pathlib_mate import Path

dir_here = Path.dir_here(__file__)

print(dir_here) # /Users/myname
Parameters:

file_var (str) – the __file__ variable

Return type:

Path