6

On Mac OS, when you "Get Info" from a file, in "More info", there's a "Where from" information that shows me the original url from where I download the file.

I want to get to this information using Python, and I cannot seem to find a way to do it.

The os.path library doesn't seem to help since it only gives me information on things like creation time, size, etc. and nothing about the original download link.

  • That might be a Apple specific thing, certainly doable somehow from Python on Mac too. Is it fine for you if it only works in Mac OS? – antont Sep 30 '20 at 20:21
  • https://stackoverflow.com/a/49567772/169854 please see this answer about the `mdls` command. It seems like exactly what you need. It's not Python, but if you insist you can call this command from Python via subprocess. This metadata is specific to Apple, so its unknown if there's a Python library specifically for this – dza Sep 30 '20 at 21:18
  • You want to access MacOS xattr (extended attributes of files/directories) in Python. – smci Jul 24 '23 at 08:33

1 Answers1

4

IMPORTANT: This answer only applies to macOS.

The additional information is stored as a "file attribute". As far as I am aware, the os.path and pathlib cannot read arbitrary attributes.

However, you can use libraries, such as xattr to read "non-standard"1 attributes.

Something like this should work2:

import xattr

value = xattr.getxattr("file.txt", "some_key").decode("utf-8")

macOS stores informations such as the "Where from" attribute under the key com.apple.metadata:kMDItemWhereFroms.


1 I consider the values returned by fstat as standard attributes. I am aware, that there is no "true" standard.

2 The code hasn't been tested yet; I left my device at home.

Julian Kirsch
  • 539
  • 4
  • 17