File created datetime wrong

Hi
I want to extract the creation datetime a file, use ‘file’ command at ubuntu linux, this datetime of creation is 2022:01:23 16:32:29, which is correct.

(base) $ file IMG_7526.JPG*
IMG_7526.JPG: JPEG image data, JFIF standard 1.01, resolution (DPI), density 300x300, segment length 20, Exif Standard: [TIFF image data, big-endian, direntries=11, manufacturer=Apple, model=iPhone 12, orientation=upper-left, xresolution=162, yresolution=170, resolutionunit=2, software=15.2.1, datetime=2022:01:23 16:32:29, hostcomputer=iPhone 12], baseline, precision 8, 4032x3024, components 3*

however is I use python os module, got a wrong created date

>>> datetime.date.fromtimestamp(os.stat(f).st_ctime)*
datetime.date(2023, 10, 14)*

somewhere it was suggest os.stat_result.st_birthtime may help. I can’t find it

>>> datetime.date.fromtimestamp(os.stat(f).st_birthtime)*
Traceback (most recent call last):*
  File "<stdin>", line 1, in <module>*
AttributeError: 'os.stat_result' object has no attribute 'st_birthtime'

The directory statistics are completely different from the EXIF data inside that file. If you want a way of parsing out that, there are some packages on PyPI that can help.

thanks a lot, using EXIF.py to get correct creation datetime

tk='EXIF DateTimeOriginal'
            ff=open (source, 'rb')
            tags=exifread.process_file(ff)
            print (source, 'TAGS')
            mtt=str(tags[tk]).split(" ")[0].replace(':', '-')

While it’s good you’re usingthe EXIF data, just a couple of remarks
about the os.stat calls:

however is I use python os module, got a wrong created date

>>> datetime.date.fromtimestamp(os.stat(f).st_ctime)*
datetime.date(2023, 10, 14)*

The st_ctime is not the creation time. Technically, it is “last change
to inode”, which includes chmods, chowns etc i.e. changes to the file’s
OS metadata. Plenty of people think the c means creation, but it does
not.

somewhere it was suggest os.stat_result.st_birthtime may help. I can’t find it

>>> datetime.date.fromtimestamp(os.stat(f).st_birthtime)*
Traceback (most recent call last):*
 File "<stdin>", line 1, in <module>*
AttributeError: 'os.stat_result' object has no attribute 'st_birthtime'

Linux (and POSIX) doesn’t specify this field. And it would require
support by the underlying filesystem too.

I gather from this stackoverflow answer that BSD UNIXen have it:

(and MacOS, which is BSD derived). OTOH, an ancient OpenBSD 6.1 system
here (with an equally ancient Python 2.7.13) doesn’t have it.

Anyway, the EXIF data are better - for example, they’ll survive a file
copy, which would otherwise create a young looking file.