bag.pathlib_complement module

A subclass of Path from pathlib.

Python 3.4 introduces an object-oriented module for path manipulation called pathlib. But it is missing certain convenience methods.

class bag.pathlib_complement.Path[source]

Bases: pathlib.PosixPath

pathlib.Path subclass – has more methods.

chgrp(gid)[source]

Change the UNIX group of this path.

copy(dest)[source]
empty()[source]

Remove directory contents without removing the directory itself.

ensure_directory(parents=True)[source]

Create the directory only if it does not yet exist.

recursive_chgrp(group, this=False)[source]

Change the UNIX group of the directory contents.

If this, changes the directory itself, too.

recursive_chmod(file_perms, dir_perms=None)[source]
remove()[source]

Delete self, irrespective of whether it’s symlink, file or dir.

walk(filter=None, this=False)[source]

Recursively traverse this directory.

bag.pathlib_complement.corresponding_directory_perm(perm)[source]

Given 4, returns 5. Given 6, returns 7.

Based on desired file permissions, returns corresponding dir permissions.

bag.pathlib_complement.default_directory_perms(file_perms)[source]

Given file_perms, return directory permissions.

Most people understand UNIX permissions for files, but not for directories – if a file has 644 then usually its parent should be 754. We automate this here.

bag.pathlib_complement.oct2int(number)[source]

Convert 3 numbers to be able to chmod.

Given a (string or int) representation of a file mode, such as “777” – you have to realize that is actually an octal number –, returns the corresponding integer to be able to chmod.