/Common-Path

Python package to evaluate the most common file path from a list of paths.

Primary LanguagePythonMIT LicenseMIT

Travis Coveralls Conda PyPI

Common Path

Python package to evaluate the most common file path from a list of paths.

Say you have a typical file history list:

>>> history = [
...     'C:\\Users\\Adam Smith\\Documents\\doc1.txt',
...     'C:\\Users\\Adam Smith\\Documents\\doc2.txt',
...     'C:\\Users\\log.txt',
...     'D:\\doc4.txt',
...     'C:\\Users\\Adam Smith\\Documents\\doc3.txt',
...     'C:\\Users\\Adam Smith\\Documents\\Folder\\image.jpg'
... ]

You want to know the common path C:\Users\Adam Smith\Documents, ignoring the occasional unusual paths C:\Users and D:\:

>>> import commonpath
>>> commonpath.natural(history)
'C:\\Users\\Adam Smith\\Documents'

Want to limit the path depth?

>>> commonpath.natural(history, max_depth=3)
'C:\\Users\\Adam Smith'

If you really want to know the absolute most common path:

>>> commonpath.most(history)
'C:\\Users'

Or you want to know what path they all have in common?

>>> commonpath.common(history)

As in: none! We'll need to get rid of D:\\doc4.txt to get anything in common:

>>> history.pop(3)
'D:\\doc4.txt'
>>> commonpath.common(history)
'C:\\Users'

Licence

Distributed under the MIT licence.