1

Which python libraries can be used to get detailed file attributes on a MacOSX file system? I have a hunch that it uses the os module, but I've been unable to gain access to these attributes through python.

The most important attribute I want to access would be file 'kind'. Note that I'm not talking about the extension, but rather the 4th column from the screenshot below (taken from this SuperUser question). For example, when I loop through these files in this directory, how can I get "Plain text document" as the kind for license.txt? My application is a file-handling script that should discern directories from keynote/pages/numbers package files.

A note on duplication:

Also note that I have seen this particular SO question, which is in respect to general *nix file systems. My question is a subset of this question for MacOS file systems which may have some idiosyncrasies not discussed in that question. If a user can supply an example of HOW python-magic can be used to get the file kind, then this will provide a more useful solution than the aforementioned article.

enter image description here

Thomas Matthew
  • 2,826
  • 4
  • 34
  • 58
  • Does this help? https://stackoverflow.com/questions/1974724/is-there-a-python-equivalent-of-the-unix-file-utility – PM 2Ring May 03 '18 at 07:07
  • If you just want to distinguish between plain files and directories, see [`os.path.isfile`](https://docs.python.org/3/library/os.path.html#os.path.isfile) and `os.path.isdir`. Also see [Path.is_dir](https://docs.python.org/3/library/os.path.html#os.path.isfile). – PM 2Ring May 03 '18 at 07:11
  • @PM2Ring I'm not sure how to use that SO article that you sent me for my specific purpose -- that will take some time to figure out. `os.path.isfile` and `os.path.isdir` don't work because the package files I'm using are incorrectly seen as directories, and I want to handle these types of files and NOT actual directories, so I have to rely on file kind instead. – Thomas Matthew May 03 '18 at 07:24
  • Sorry, I'm not that familiar with MacOSX. `file` is a standard *nix command, you can read about it [here](https://linux.die.net/man/1/file). I assume Mac has `file`, although Mac does some "special" things with the way it handles files. – PM 2Ring May 03 '18 at 07:40
  • Also see https://stackoverflow.com/a/1974737/1110636 – Timir May 03 '18 at 16:50
  • Possible duplicate of [Is there a python-equivalent of the unix "file" utility?](https://stackoverflow.com/questions/1974724/is-there-a-python-equivalent-of-the-unix-file-utility) – Timir May 03 '18 at 16:50

1 Answers1

0

You can get the last modified date and the file size in one go from os.stat().

File kind is more complicated, as there are at least two different ways to get a human readable description from the OS. For this, the library you want is pyobjc, which will allow you to a) query NSWorkspace and LaunchServices for UTI related data, or b) create NSURLs for individual files and query them for their resource type. I believe b) is closer to what Finder does.

Here's an example illustrating both of these methods:

from AppKit import NSWorkspace
from LaunchServices import UTTypeCopyDescription

from Cocoa import NSURL, NSURLLocalizedTypeDescriptionKey

from datetime import datetime
import os

for f in os.listdir("."):
    uti = NSWorkspace.sharedWorkspace().typeOfFile_error_(f, None)[0]
    desc = UTTypeCopyDescription(uti)

    url = NSURL.fileURLWithPath_(f)
    urlType = url.getResourceValue_forKey_error_(None, NSURLLocalizedTypeDescriptionKey, None)[1]

    sr = os.stat(f)
    size = sr.st_size
    date = datetime.fromtimestamp(sr.st_mtime).strftime('%Y-%m-%d %H:%M:%S')

    print("%-20s size=%s modified=%s uti=%s desc=%s url=%s" % (f, size, date, uti, desc, urlType))
snowcat
  • 284
  • 1
  • 5