Last active
November 7, 2016 16:31
-
-
Save mherkazandjian/144d0b1814b2256cbe3ebae4b78495d9 to your computer and use it in GitHub Desktop.
function that splits a path into all its components
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def split_path(path): | |
"""split a path fully into its components | |
:param path: input path string | |
:return: list of the split path | |
.. todo:: see how to use generators with the yield statement | |
.. todo:: to replace the while loop | |
.. code-block:: python | |
# split a path into all its components | |
my_path = '/path/to/foo' | |
print(split_path(my_path)) | |
>>> ['/', 'path', 'to', 'foo'] | |
# generate the input path from the split path | |
assert my_path == os.path.join(*split_path(my_path)) | |
>>> True | |
""" | |
retval = [] | |
path_head, path_tail = os.path.split(path) | |
while path_tail != '': | |
retval.insert(0, path_tail) | |
path_head, path_tail = os.path.split(path_head) | |
retval.insert(0, path_head) | |
return retval |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment