Search code examples
pythonfiledatecreation

Extract file creation date via Python


I ran into an issue when trying to extract file creation date via Python. I am using Python3.6 on 64bit Windows7.

I have a number of pictures (jpeg) that were taken [I should use "created" instead] in the past and uploaded to an on-line album on the same day. [Note: These JPEGs are not 'fresh' off the camera, some of them have been processed by other software and then "saved as"; Some of them are actually cellphone screenshots. So the EXIF Info may not be available] For example one of them were taken [or a better example cellphone screenshot created] and uploaded on 1/1/2016. I downloaded it from the on-line album on 11/1/2017. When this jpeg file is listed in Win7 file explorer’s “details” view, under “date” column it is showing 1/1/2016. When I try to use Python to extract the creation date info, all three ways (st_atime, st_mtime, st_ctime) gave me 11/1/2017. I then right click the file and view its property, there “created”, “modified”, and “accessed” all pointed to 11/1/2017.

I am wondering if there is still a Python way to get the original creation date, i.e. 1/1/2016, from this file? On my windows screen I clearly see that date "1/1/2016" Why I could not retrieve it? That's thing really drives me to ask this question

Thank you!


Solution

  • You've merged some dates, so let's clarify which one you want. Consider this sequence:

    1. photographer (perhaps you) takes photo on 04 Jan;

    2. uploads to photographer's computer on 05 Jan;

    3. uploads to on-line album on 07 Jan.

    4. You find the file on 09 Jan and download to your album.

    Now, which date do you want to extract? BTW, the 05 Jan date is almost certainly unavailable, unless you have access to the photographer's disk.

    The 04 Jan date is available as shown in the link @EsotericVoid gave you; in that case, we should remove this question as a duplicate.

    09 Jan is already available to you -- as you've lamented. :-)

    For 07 Jan, you'll need to look up how to preserve dates in whatever download command you're using; most have an option that will keep the dates from the file source (creation, modification, and access).


    Update per OP's clarification

    Understood: you need the original photo date. That will be recorded only in the original image file. Since you have different file formats, you'll need to write code that will first determine the format, and then extract the date per that particular format. You now have a link to the EXIF solution.