How to change the creation date of file using python on a mac?

Question:

I need to update the creation time of a .mp4 file so that it will appear at the top of a list of media files sorted by creation date. I am able to easily update both the accessed and modified date of the file using os.utime, but have yet to find a good way to change the created date of a file to “now”.

My end goal is to seed media files to an iOS simulator using appium, and have those media files accessible in that script. The issue is that the video file will not be displayed in the “recently added” section of the app because it is several days old.

Asked By: Eric

||

Answers:

I was able to successfully use the subprocess call command to change the creation date of the file with a shell command.

from subprocess import call

command = 'SetFile -d ' + '"05/06/2019 "' + '00:00:00 ' + complete_path
call(command, shell=True)
Answered By: Eric

This will also do the trick and I find it a bit cleaner:

import os
os.system('SetFile -d "{}" {}'.format(date.strftime('%m/%d/%Y %H:%M:%S'), filePath))
Answered By: dontic

Combining @Eric’s subprocess call and @dontic’s strftime with my way of string manipulation for copy-paste. Thank you two for the solution.

from subprocess import call    

command = 'SetFile -d "%s" "%s"' % (date.strftime('%m/%d/%Y %H:%M:%S'), filePath)
call(command, shell=True)
Answered By: Ferris S