Created
March 8, 2011 18:30
-
-
Save goozbach/860716 to your computer and use it in GitHub Desktop.
Iterate over the entire path one part at a time.
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
| #!/usr/bin/env python3 | |
| import os | |
| mainpath = 'foo/bar/baz/qux/garple/sux/' | |
| def incrementally_joined(parts): | |
| full = '' | |
| for p in parts: | |
| full += p + os.sep | |
| yield full | |
| def pathcrement(mypath): | |
| #strip off any os.sep on either end of the path, and split into an array | |
| return os.path.splitdrive(mypath)[1].lstrip(os.sep).rstrip(os.sep).split(os.sep) | |
| for path in incrementally_joined(pathcrement(mainpath)): | |
| print(path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment