Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As another answer mentions, Mac OS in the documentation means pre-OS X. In classic Mac OS, <code>stat</code> was emulated, whereas Python <code>stat</code> actually means <code>stat</code> on OS X.</p> <p>Therefore, you don't get the type and creator with os.stat. The corresponding low-level call is <code>getattrlist</code>, but this isn't wrapped by Python (nor should you really use it). You can see exactly what is returned by <code>stat</code> by printing it:</p> <pre><code>&gt;&gt;&gt; import os &gt;&gt;&gt; s = os.stat('foo') &gt;&gt;&gt; s posix.stat_result(st_mode=33152, st_ino=18294505, st_dev=16777218L, st_nlink=1, st_uid=501, st_gid=501, st_size=0, st_atime=1379052292, st_mtime=1379052292, st_ctime=1379052308) </code></pre> <p>Let's set a type and creator on <code>foo</code> to test with:</p> <pre><code>% SetFile -t 'TYPE' -c 'CREA' foo </code></pre> <p>The easy answer in Python 2.x is <code>MacOS.GetCreatorAndType</code>. However, it's gone in 3.x and the underlying mechanisms it uses are deprecated. If your goal is just to get something done for yourself, then by all means use it.</p> <pre><code>&gt;&gt;&gt; import MacOS &gt;&gt;&gt; MacOS.GetCreatorAndType('foo') ('CREA', 'TYPE') </code></pre> <p>A more future-proof mechanism would be to use PyObjC, which works on Python 3.x and does not use deprecated OS X API:</p> <pre><code>&gt;&gt;&gt; from Foundation import NSFileManager, NSFileHFSCreatorCode, NSFileHFSTypeCode &gt;&gt;&gt; attributes = NSFileManager.defaultManager().attributesOfItemAtPath_error_('foo', None)[0] &gt;&gt;&gt; attributes[NSFileHFSTypeCode] 1415139397L &gt;&gt;&gt; attributes[NSFileHFSCreatorCode] 1129465153L </code></pre> <p>These come out as integers because PyObjC has no mapping for four-character codes (nor does it have the appropriate metadata to understand what it's getting). Here's a quick mapping function to retrieve a four-character string:</p> <pre><code>&gt;&gt;&gt; def decode(f): return ''.join(chr(f &gt;&gt; i * 8 &amp; 0xff) for i in xrange(3,-1,-1)) ... &gt;&gt;&gt; decode(attributes[NSFileHFSCreatorCode]) 'CREA' &gt;&gt;&gt; decode(attributes[NSFileHFSTypeCode]) 'TYPE' </code></pre> <p>(Replace <code>xrange</code> with <code>range</code> above if you're using Python 3.)</p> <p>Note that in general, types/creators are not used very much in OS X; their functions have been superseded by file extensions, UTIs, MIME types, per-user and per-document application bindings.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload