Skip to main content

An extended and more powerful pathlib.

Project description

https://travis-ci.org/MacHu-GWU/pathlib_mate-project.svg?branch=master https://img.shields.io/pypi/v/pathlib_mate.svg https://img.shields.io/pypi/l/pathlib_mate.svg https://img.shields.io/pypi/pyversions/pathlib_mate.svg

Welcome to pathlib_mate Documentation

pathlib is an awesome library handling path in different OS. And it’s been added into standard library since Python3.4. pathlib_mate gives extensive methods and attributes, makes pathlib more powerful and user-friendly.

Install

pathlib_mate is released on PyPI, so all you need is:

$ pip install pathlib_mate

To upgrade to latest version:

$ pip install --upgrade pathlib_mate

Documentation

Extended Attributes

First, let’s use a simple file for demonstration C:\Users\admin\readme.txt:

>>> from pathlib_mate import Path
>>> p = Path(r"C:\Users\admin\readme.txt")

pathlib_mate provides a set of very straightforward attribute name:

>>> p.abspath # Absolute path.
'C:\Users\admin\readme.txt'

>>> p.dirpath # Parent dir full absolute path.
'C:\Users\admin'

>>> p.dirname # Parent dir name.
'admin'

>>> p.basename # File name with extension, path is not included.
'readme.txt'

>>> p.fname # File name without extension.
'readme'

>>> p.txt # File extension. If it's a dir, then return empty str.
'.txt'

>>> p.md5 # md5 check sum of this file (if exists)

>>> p.size # size in bytes
1873

>>> p.size_in_text # human readable string of the file size
'1.83 KB'

>>> p.mtime # Most recent modify time in timestamp. (atime, ctime is similar)
1451624400

>>> p.modify_datetime # Most recent modify time in datetime.
datetime.datetime(2016, 1, 1, 5, 0) # (access_datetime, create_datetime is similar)

Extended Method

The default Path.rename(target) method is not good enough. pathlib_mate provide a new utility method Path.moveto(new_abspath=None, new_dirpath=None, new_dirname=None, new_fname=None, new_ext=None) making rename way more easier.

# You can easily update any parts of the path
# Plus a new Path instance will return
>>> p_new = p.moveto(new_dirpath=r"C:\User\guest")
>>> p_new
'C:\User\guest\readme.txt'

>>> p_new = p.moveto(new_fname=r"introduction")
>>> p_new
'C:\User\guest\introduction.txt'

And you can use Path.remove() to remove the file form your disk, if it is a file.

>>> p.remove()

Selecting specific files from a directory, and sorting the result set, is very frequently used. But the Path.glob() and Path.iterdir() is not good enough. Let’s see how easy it’s done with pathlib_mate, and it’s super powerful.

>>> path = Path(r"C:\User\admin")

# This select files recursively in a directory
>>> for p in path.select_file():
...

# This select directories recursively in a directory
>>> for p in path.select_dir():
...

If you don’t want to include the subfolders (non-recursively), set recursive=False.

>>> for p in path.select_file(recursive=False):
...

You can easily customize the rules you use for filtering. You only need to define a filter function like this:

def filter_image_file(p):
    """This filter returns True only if it is a .jpg and .png file.
    """
    return p.ext.lower() in [".jpg", ".png"]

# Filter image file
>>> for p in path.select_file(filter_image_file):
...

Plus, pathlib_mate provides a set of utility methods for selection (They all support the recursive keyword):

  • select_by_ext(ext=[".jpg", ".png"]): Select file path by extension.

  • select_by_pattern_in_fname(pattern="001"): Select file path by text pattern in file name.

  • select_by_pattern_in_abspath(pattern="admin"): Select file path by text pattern in absolute path.

  • select_by_size(min_size=0, max_size=999999999): Select file path by size.

  • select_by_mtime(min_time=0, max_time=999999999): Select file path by modify time.

  • select_by_atime(min_time=0, max_time=999999999): Select file path by access time.

  • select_by_ctime(min_time=0, max_time=999999999): Select file path by create time.

  • select_image(): Select image file.

  • select_audio(): Select audio file.

  • select_video(): Select video file.

  • select_word(): Select compressed archive file.

  • select_excel(): Select Microsoft Excel file.

  • select_archive(): Select compressed archive file.

Sort result set

Sort set of path is super easy in pathlib_mate:

result = path.select_file()
# sort file by its size, from largest to smallest
>>> for p in Path.sort_by_size(result, reverse=True):
...

In addition, you have these options for sorting.

  • Path.sort_by_abspath

  • Path.sort_by_fname

  • Path.sort_by_ext

  • Path.sort_by_size

  • Path.sort_by_mtime

  • Path.sort_by_atime

  • Path.sort_by_ctime

  • Path.sort_by_md5

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pathlib_mate-0.0.2.zip (75.8 kB view details)

Uploaded Source

File details

Details for the file pathlib_mate-0.0.2.zip.

File metadata

  • Download URL: pathlib_mate-0.0.2.zip
  • Upload date:
  • Size: 75.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pathlib_mate-0.0.2.zip
Algorithm Hash digest
SHA256 a7fd0f4af406328d0dd63bd6b2c7d4e123b591f58d5ad51aedcd9cdd2fc4bbb9
MD5 89ed7674d053ebbea32496cf07fb5c3a
BLAKE2b-256 f540e3f994d0195d5c56b22bb1b35a550d0e2f03fd9f945ae013e9efe69d9b4f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page