2

The projects I work on usually end up having a lot of data generated in different contexts and I have to organize everything somehow.

Current solution I have is to decide upon a folder structure which segments the data up to some extend (in a folder I already know those files are supposed to share few properties), and then I encode the rest of the information in the file name, separating fields with "_" etc.

However I think this solution is bit hacky and not well organized. And if somehow someone(me) accidentally drops a file to a wrong folder, in specific cases it might go unnoticed etc. (or I have to encode every field to the file name). Another solution I thought of is to generate id's as file names and keep an index file translating an id (file name) to set of properties. However if I lose that file, that would have been a disaster.

For that reason, specifically, I am looking for a way to add metadata (eg arbitrary attributes in json format) to a file without changing its name, so name could still be a high level description/id. I use mac and see comments when I click info on any file I have (see attachment). So if I can modify that field or do something similar (preferably something that would work in linux too), I could save a json as text there and parse it whenever I need that.

Is there a simple way to achieve that using python?

comments field

Note: solution I am looking for is not this answer, because the change is not saved with the file.

ozgeneral
  • 6,079
  • 2
  • 30
  • 45
  • I think what you're looking for is this: https://stackoverflow.com/questions/27759204/how-to-write-os-x-finder-comments-from-python – Pvham Jan 28 '19 at 10:50
  • thanks, I'll try that! but I'm keeping the question open in case someone posts a nicer solution – ozgeneral Jan 28 '19 at 11:06
  • 1
    I think you're looking for [Extended file attributes](https://en.wikipedia.org/wiki/Extended_file_attributes). See https://stackoverflow.com/a/33182025/1451664. – Chris Jan 28 '19 at 13:23

1 Answers1

1

One way to do it is to simply write down data to the end of image and read it when required:

import re
import json

def add_meta(image, meta):
    with open(image, 'a+b') as f:
        f.write(json.dumps(meta).encode('utf-8'))

def read_meta(image):
    with open(image, 'rb') as f:
        data = str(f.read())
    meta = re.findall(r'xff.*({.*})\'\Z', data)[-1]
    return meta

add_meta('image.jpg', {'Description': 'SO'})
print(read_meta('image.jpg'))

It gives you on output:

{"Description": "SO"}

Drawback of this approach is that metadata is not visible in comments section of image.

Alderven
  • 7,569
  • 5
  • 26
  • 38