[Pythonmac-SIG] Reading the resource fork with Python?

Michael Hudson mwh at python.net
Tue Feb 1 13:32:05 CET 2005


Bill Janssen <janssen at parc.com> writes:

> Given a filename on an HFS volume, what call do I use to see if it has
> a resource fork, and if there's anything in it?  How do I read that
> data?

rpath = os.path.join(yourpath, '..namedfork', 'rsrc')
os.path.getsize(rpath) > 0
data = open(rpath).read()

or, possibly more helpfully, 

from Carbon import Res

try:
    rf = Res.FSpOpenResFile(yourpath, 0)
except MacOS.Error, e:
    if e.args[0] == -39:
        # not a resource file
        pass
    else:
        raise
else:
    for i in range(1, Res.Count1Types() + 1):
        typ = Res.Get1IndType(i)
        for j in range(1, Res.Count1Resources(typ) + 1):
            res = Res.Get1IndResource(typ, j)
            id_, typ2, name = res.GetResInfo()
            print repr(typ), id_, repr(name), len(res.data), 
            if len(res.data) < 40:
                print repr(res.data)
            else:
                print
    Res.CloseResFile(rf)

> I'm trying to read the URL out of a .webloc file, if that helps.

Running the above on the only webloc file I seem to have lying around
gives:

'drag' 128 '' 48
'TEXT' 256 '' 31 'http://www.ambrosiasw.com/news/'
'url ' 256 '' 31 'http://www.ambrosiasw.com/news/'

Cheers,
mwh

-- 
  I think if we have the choice, I'd rather we didn't explicitly put
  flaws in the reST syntax for the sole purpose of not insulting the
  almighty.                                    -- /will on the doc-sig


More information about the Pythonmac-SIG mailing list