#!/usr/bin/env python # Refer # Documentation: https://github.com/wackywendell/fpath # Source code: https://github.com/wackywendell/fpath/blob/master/fpath.py try: import fpath except ImportError: import ck fpath = ck.refs.fpath Path = fpath.Path p = Path("dir1/dir2/dir3/prefix.middle.extension") print p # /dir1/dir2/dir3/prefix.middle.extension for part in p: print part # / # dir1 # dir2 # dir3 # prefix.middle.extension print p + "further/down/the/rabbit/hole" # dir1/dir2/dir3/prefix.middle.extension/further/down/the/rabbit/hole # Same as above print p + ("further", "down", "the", "rabbit", "hole") # dir1/dir2/dir3/prefix.middle.extension/further/down/the/rabbit/hole try: print p + "/further/down/the/rabbit/hole" except StandardError, e: print "Expected an exception:", e # You can't code this syntax though. try: print p + ("/", "further", "down", "the", "rabbit", "hole") except StandardError, e: print "Expected an exception:", e # extension print p.extension # extension # Get last part of the path. print p[-1] # prefix.middle.extension # dirname print p[:-1] # dir1/dir2/dir3 # os.path.isdir print Path("/tmp").stat().isdir # True print Path("/tmp").stat().isfile # False # Walking # # You can't walk path objects. # Call .transform() to get the Dir() or File() corresponding to that Path # Dir objects have the walk attribute. tmp = Path("/tmp").transform() # It's kind of lame so use the os.walk variant instead.