4

Hello it's my first post.

I started doing some coding in Python on OS X today.

I've noticed that OS X has such thing as a date added which is the time the file was put into specified folder.

I'm trying to get that date as a timestamp, however none of types work.

I've tried all three I know:

st = os.path.getctime('Untitled.gif') st1 = os.path.getatime('Untitled.gif') st2 = os.path.getmtime('Untitled.gif') 

But none of them shows what I need. Maybe there's a way to get all metadata from file somehow and just pick the info I need.

Thanks

1

3 Answers 3

3

You can get this information with the mdls command, called via subprocess:

import subprocess st = subprocess.check_output(["mdls", "-name", "kMDItemDateAdded", "-raw", "Untitled.gif"]) 
Sign up to request clarification or add additional context in comments.

Comments

2

It seems st_ctime (the time of most recent metadata change on Unix) is the closest attribute to "Date Added" on Mac that the class os.stat_result offers—which at a time resembles "Date Added" for some files yet at another time doesn't for the same files!

import os import datetime st_result = os.stat('Untitled.gif') ts = st_result.st_ctime dt = datetime.datetime.fromtimestamp(ts) print(dt) 

Sample output:

2021-09-08 15:10:22.602768 

If I wish to interpret st_ctime as "Date Added," I should manually check if it correctly holds "Date Added" for all the intended files—e.g. sorting the files in a terminal by ctime with ls -ltc | head -n NUMBER_OF_LINES_I_WANT_TO_SEE; then corresponding their order with the order I see on a finder sorted by "Date Added."

Comments

0

Try os.stat('Untitled.gif').st_birthtime. More info on os.stat can be found here.

1 Comment

Birthtime is file creation date, not the date added to the directory.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.