[Pythonmac-SIG] converting file paths between HFS and POSIX formats

Nicholas Riley njriley at uiuc.edu
Fri Apr 22 16:56:10 CEST 2005


On Fri, Apr 22, 2005 at 02:05:28PM +0100, has wrote:
> Just wondering if there's anything in MacPython for converting HFS
> paths to POSIX and vice-versa, or is my memory playing tricks on me?
> If not, does anyone have some code they don't mind sharing?

Keep in mind that HFS paths aren't unique, whereas POSIX paths are, so
you should be using FSRefs, CFURLs, POSIX paths or aliases for
canonical representations, cognizant of all the tradeoffs between
those formats.

In [1]: from Carbon.CoreFoundation import *

In [2]: from Carbon.CF import *

In [3]: u = CFURLCreateFromFileSystemRepresentation('/Applications', False)

In [4]: u.CFURLCopyFileSystemPath(kCFURLPOSIXPathStyle).toPython()
Out[4]: u'/Applications'

In [5]: u.CFURLCopyFileSystemPath(kCFURLHFSPathStyle).toPython()
Out[5]: u'Onyx:Applications'

If you _really_ must go from an imprecise HFS path to something else:

In [5]: u = toCF('Onyx:').CFURLCreateWithFileSystemPath(kCFURLHFSPathStyle, False)

In [6]: u.CFURLCopyFileSystemPath(kCFURLPOSIXPathStyle).toPython()
Out[6]: u'/Applications'

and so forth.

-- 
Nicholas Riley <njriley at uiuc.edu> | <http://www.uiuc.edu/ph/www/njriley>


More information about the Pythonmac-SIG mailing list