Python Forum
Getting Correct Creation Time
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting Correct Creation Time
#1
Hey, guys,

Very much a beginner at coding.

I was trying to rename image files by extracting their creation date using the "os.path.getctime(path)" method, but I've run into a snag.

Using Windows...

I copied the files from an external drive to my laptop. In Explorer, the creation dates still display as when the images and video were actually created: 2012

When I use "os.path.getctime(path)" on any one of those files, the creation date returned is the date I copied the files to my laptop (which was yesterday).

Is there a different function (not sure if that's the correct terminology) that I should use to extract the date/time that's the same as what's being displayed in Explorer? The ACTUAL creation date?

Thanks!

John
Reply
#2
Just to confirm, you're running this on windows only? You don't have a mac laptop or trying to use something like cygwin or WSL?
Reply
#3
You could perhaps have a look at the member os.stat_result.st_file_attributes.
Reply
#4
(Aug-10-2020, 05:27 PM)bowlofred Wrote: Just to confirm, you're running this on windows only? You don't have a mac laptop or trying to use something like cygwin or WSL?

Yes, it's WIN 10

(Aug-10-2020, 05:28 PM)Gribouillis Wrote: You could perhaps have a look at the member os.stat_result.st_file_attributes.

THANKS!

I'll give that a try!
Reply
#5
Now that I'm back near my windows machine, I can't reproduce this. I'm getting the same value for ctime as I do in explorer for creation time.

>>> str(datetime.fromtimestamp(os.path.getctime("jsony.py")))
'2020-05-13 23:24:50.830698'
>>> str(datetime.fromtimestamp(os.stat("jsony.py").st_ctime))
'2020-05-13 23:24:50.830698'
>>> str(datetime.fromtimestamp(os.path.getmtime("jsony.py")))
'2020-08-10 20:47:08.854168'
>>> str(datetime.fromtimestamp(os.stat("jsony.py").st_mtime))
'2020-08-10 20:47:08.854168'
The dates are the same as seen in the command prompt with dir /t:c (create time) and dir /t:w (write time).

Is your file on a regular NTFS volume, or maybe it's a thumb drive that only has a FAT filesystem?
Reply
#6
(Aug-11-2020, 04:04 AM)bowlofred Wrote: Now that I'm back near my windows machine, I can't reproduce this. I'm getting the same value for ctime as I do in explorer for creation time.

The dates are the same as seen in the command prompt with dir /t:c (create time) and dir /t:w (write time).

Is your file on a regular NTFS volume, or maybe it's a thumb drive that only has a FAT filesystem?

It's data that was originally created on an iPhone... copied to an external HD... and now copied from THERE to a Windows 10 laptop HD.

I haven't used "str(datetime.fromtimestamp(os.path.getctime("jsony.py")))" before. That might be the key to solving my issue.

Thanks for the help!
Reply
#7
When you put it on windows, you "created" the file for the first time. And explorer should show that. Does it really show an earlier time in the Created column?

Other operating systems don't usually support creation/birth time, so I don't think it can copy that information when moving to windows.

Main thing for me, does the os.path.getctime() value agree with dir /t:c?
Reply
#8
(Aug-12-2020, 09:00 PM)bowlofred Wrote: When you put it on windows, you "created" the file for the first time. And explorer should show that. Does it really show an earlier time in the Created column?

yeah, in windows explorer it shows the date as being in 2012 (which is when the picture was taken. But when I use os.path.getctime() it shows the date I copied the file over. Which was a few days ago.


(Aug-12-2020, 09:00 PM)bowlofred Wrote: Main thing for me, does the os.path.getctime() value agree with dir /t:c?

I'm gonna try dir /t:c. I'll also try your other suggestion. Be back with the result....
Reply
#9
(Aug-12-2020, 09:00 PM)bowlofred Wrote: Main thing for me, does the os.path.getctime() value agree with dir /t:c?

Here's what I get when I run dir /t:c:
[Image: view?usp=sharing]

Here's what I see in explorer:
[Image: view?usp=sharing]

The dates in Explorer are the actual creation dates; The date and time the photo was taken (which is what I want).

So I've realized that I have 2 problems...

Please understand that I'm reeeeaally new to Python and programming in general. I'm probably setting this up wrong.

1) When I use datetime.fromtimestamp(), I get an error that I haven't loaded it yet. So, I guess my question is... where do I import that from?

right now these are what I'm importing:
import os, datetime
from pathlib import Path
from time import ctime

2) When I use os.path.getctime("IMG_7001.JPG), or str(datetime.fromtimestamp(os.path.getctime("IMG_7001.JPG"))) It returns a "FileNotFound" error.

I think the issue is that I'm not setting up the "where" correctly. Meaning the path the files are located. I was gonna post what I have, but it's getting tangled (I'm throwing code at it to see what sticks) and I think I need to go back to some tutorials and start over.


- John
Reply
#10
Can you show the column header from explorer? Perhaps this is something other than creation date in the filesystem? I know it can show tag info on audio files. Perhaps this is something other than the filesystem info. Or, you probably have powershell on your machine. Run this in the powershell:

Get-Item <path to file> | Format-List That will show all the windows timestamps together at the top of the output.



Whenever you use a term, it has to be assigned, usually either from a = or an import. So for datetime, you need to import it (and this will be shown in the examples from the Datetime docs.

from datetime import datetime
You can do the equivalent of cd/chdir in your code if you want, or you can use a full pathname. Either is valid.
import os
os.chdir("C:/Users/someuser/Pictures")
# do whatever you want...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to acquire an imported file creation time thunderspeed 4 1,885 Sep-23-2021, 04:27 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020