简体   繁体   English

如何在 Mac 上使用 python 更改文件的创建日期?

[英]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.我需要更新 a.mp4 文件的创建时间,以便它出现在按创建日期排序的媒体文件列表的顶部。 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".我可以使用 os.utime 轻松更新文件的访问日期和修改日期,但尚未找到将文件的创建日期更改为“现在”的好方法。

My end goal is to seed media files to an iOS simulator using appium, and have those media files accessible in that script.我的最终目标是使用 appium 将媒体文件播种到 iOS 模拟器,并在该脚本中访问这些媒体文件。 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.我能够成功地使用 subprocess call 命令通过 shell 命令更改文件的创建日期。

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.将@Eric 的子进程调用和@dontic 的strftime 与我的复制粘贴字符串操作方式相结合。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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