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

3

7

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.

Almoner answered 6/5, 2019 at 16:7 Comment(0)
A
4

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)
Almoner answered 6/5, 2019 at 17:7 Comment(2)
access and last modified can be done by os.utime(path,(atime, mtime)) - thats not enough for your problem? os.utimeLubumbashi
No, in my case updating the access and modified time wasn't enough. The application was exclusively checking the creation time.Almoner
T
0

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))
Talavera answered 23/12, 2021 at 21:27 Comment(1)
subprocessing module is preferred to os.system.Approver
R
0

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)
Redon answered 23/8, 2022 at 21:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.