Skip to content

Instantly share code, notes, and snippets.

@jacob414
Created April 11, 2012 08:57
Show Gist options
  • Save jacob414/2358083 to your computer and use it in GitHub Desktop.
Save jacob414/2358083 to your computer and use it in GitHub Desktop.
Swiss army path function
import os
def path(*segs):
"""
CWD and `~` aware path construction. Start a segment with `/` for absolute paths.
*(Example, on my machine)*
>>> path('sub', 'dir')
/Users/jacob/src/tmp/sub/dir
>>> path('~', 'src', 'project', 'pkg')
/Users/jacob/src/project/pkg
"""
cand = os.path.expanduser(os.path.join(*segs))
if cand[0] != '/':
cand = os.path.abspath(cand)
return cand
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment