Skip to content

Instantly share code, notes, and snippets.

@goozbach
Created March 8, 2011 18:30
Show Gist options
  • Select an option

  • Save goozbach/860716 to your computer and use it in GitHub Desktop.

Select an option

Save goozbach/860716 to your computer and use it in GitHub Desktop.
Iterate over the entire path one part at a time.
#!/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