Paths¶
flutils offers the following path utility functions:
chmod(path, mode_file=None, mode_dir=None, include_parent=False)[source]¶Change the mode of a path.
This function processes the given
pathwithnormalize_path.If the given
pathdoes NOT exist, nothing will be done.This function will NOT change the mode of:
symlinks (symlink targets that are files or directories will be changed)
sockets
fifo
block devices
char devices
- Parameters
path (
str,bytesorPath) – The path of the file or directory to have it’s mode changed. This value can be a glob pattern.mode_file (
int, optional) – The mode applied to the givenpaththat is a file or a symlink target that is a file. Defaults to0o600.mode_dir (
int, optional) – The mode applied to the givenpaththat is a directory or a symlink target that is a directory. Defaults to0o700.include_parent (
bool, optional) – A value ofTrue`will chmod the parent directory of the givenpaththat contains a a glob pattern. Defaults toFalse.- Return type
Examples
>>> from flutils.pathutils import chmod >>> chmod('~/tmp/flutils.tests.osutils.txt', 0o660)Supports a glob pattern. So to recursively change the mode of a directory just do:
>>> chmod('~/tmp/**', mode_file=0o644, mode_dir=0o770)To change the mode of a directory’s immediate contents:
>>> chmod('~/tmp/*')
chown(path, user=None, group=None, include_parent=False)[source]¶Change ownership of a path.
This function processes the given
pathwithnormalize_path.If the given
pathdoes NOT exist, nothing will be done.
- Parameters
path (
str,bytesorPath) – The path of the file or directory that will have it’s ownership changed. This value can be a glob pattern.user (
strorint, optional) – The “login name” used to set the owner ofpath. A value of'-1'will leave the owner unchanged. Defaults to the “login name” of the current user.group (
strorint, optional) – The group name used to set the group ofpath. A value of'-1'will leave the group unchanged. Defaults to the current user’s group.include_parent (
bool, optional) – A value ofTruewill chown the parent directory of the givenpaththat contains a glob pattern. Defaults toFalse.- Raises
- Return type
Examples
>>> from flutils.pathutils import chown >>> chown('~/tmp/flutils.tests.osutils.txt')Supports a glob pattern. So to recursively change the ownership of a directory just do:
>>> chown('~/tmp/**')To change ownership of all the directory’s immediate contents:
>>> chown('~/tmp/*', user='foo', group='bar')
directory_present(path, mode=None, user=None, group=None)[source]¶Ensure the state of the given
pathis present and a directory.This function processes the given
pathwithnormalize_path.If the given
pathdoes NOT exist, it will be created as a directory.If the parent paths of the given
pathdo not exist, they will also be created with themode,userandgroup.If the given
pathdoes exist as a directory, themode,user, and :groupwill be applied.
- Parameters
mode (
int, optional) – The mode applied to thepath. Defaults to0o700.user (
strorint, optional) – The “login name” used to set the owner of the givenpath. A value of'-1'will leave the owner unchanged. Defaults to the “login name” of the current user.group (
strorint, optional) – The group name used to set the group of the givenpath. A value of'-1'will leave the group unchanged. Defaults to the current user’s group.- Raises
ValueError – if the given
pathcontains a glob pattern.ValueError – if the given
pathis not an absolute path.FileExistsError – if the given
pathexists and is not a directory.FileExistsError – if a parent of the given
pathexists and is not a directory.- Return type
PosixPathorWindowsPathdepending on the system.Example
>>> from flutils.pathutils import directory_present >>> directory_present('~/tmp/test_path') PosixPath('/Users/len/tmp/test_path')
exists_as(path)[source]¶Return a string describing the file type if it exists.
This function processes the given
pathwithnormalize_path.
- Parameters
path (
str,bytesorPath) – The path to check for existence.- Return type
''(empty string): if the givenpathdoes NOT exist; or, is a broken symbolic link; or, other errors (such as permission errors) are propagated.
'directory': if the givenpathpoints to a directory or is a symbolic link pointing to a directory.
'file': if the givenpathpoints to a regular file or is a symbolic link pointing to a regular file.
'block device': if the givenpathpoints to a block device or is a symbolic link pointing to a block device.
'char device': if the givenpathpoints to a character device or is a symbolic link pointing to a character device.
'FIFO': if the givenpathpoints to a FIFO or is a symbolic link pointing to a FIFO.
'socket': if the givenpathpoints to a Unix socket or is a symbolic link pointing to a Unix socket.Example
>>> from flutils.pathutils import exists_as >>> exists_as('~/tmp') 'directory'
find_paths(pattern)[source]¶Find all paths that match the given glob pattern.
This function pre-processes the given
patternwithnormalize_path.
- Parameters
pattern (
str,bytesorPath) – The path to find; which may contain a glob pattern.- Return type
- Yields
Example
>>> from flutils.pathutils import find_paths >>> list(find_paths('~/tmp/*')) [PosixPath('/home/test_user/tmp/file_one'), PosixPath('/home/test_user/tmp/dir_one')]
get_os_group(name=None)[source]¶Get an operating system group object.
- Parameters
name (
strorint, optional) – The “group name” orgid. Defaults to the current users’s group.- Raises
- Return type
A tuple like object.
Example
>>> from flutils.pathutils import get_os_group >>> get_os_group('bar') grp.struct_group(gr_name='bar', gr_passwd='*', gr_gid=2001, gr_mem=['foo'])
get_os_user(name=None)[source]¶Return an user object representing an operating system user.
- Parameters
name (
strorint, optional) – The “login name” oruid. Defaults to the current user’s “login name”.- Raises
- Return type
A tuple like object.
Example
>>> from flutils.pathutils import get_os_user >>> get_os_user('foo') pwd.struct_passwd(pw_name='foo', pw_passwd='********', pw_uid=1001, pw_gid=2001, pw_gecos='Foo Bar', pw_dir='/home/foo', pw_shell='/usr/local/bin/bash')
normalize_path(path)[source]¶Normalize a given path.
The given
pathwill be normalized in the following process.
byteswill be converted to astrusing the encoding given bygetfilesystemencoding().
PosixPathandWindowsPathwill be converted to astrusing theas_posix()method.An initial component of
~will be replaced by that user’s home directory.Any environment variables will be expanded.
Non absolute paths will have the current working directory from
os.getcwd()to change the current working directory before calling this function.Redundant separators and up-level references will be normalized, so that
A//B,A/B/,A/./BandA/foo/../Ball becomeA/B.
- Parameters
- Return type
PosixPathorWindowsPathdepending on the system.Example
>>> from flutils.pathutils import normalize_path >>> normalize_path('~/tmp/foo/../bar') PosixPath('/home/test_user/tmp/bar')
path_absent(path)[source]¶Ensure the given
pathdoes NOT exist.New in version 0.4.
If the given
pathdoes exist, it will be deleted.If the given
pathis a directory, this function will recursively delete all of the directory’s contents.This function processes the given
pathwithnormalize_path.Example
>>> from flutils.pathutils import path_absent >>> path_absent('~/tmp/test_path')