简体   繁体   中英

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

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.

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)

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))

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM