如何使用Python设置文件的创建和修改日期/时间?
要获取文件的创建时间,可以在Windows上使用os.path.getctime(file_path)。在UNIX系统上,您不能使用与上次更改文件属性或内容时返回的函数相同的函数。要在基于UNIX的系统上获得创建时间,请使用stat元组的st_birthtime属性。
示例
在Windows上:
>>> import os
>>> print os.path.getctime('my_file')
1505928271.0689342它以自该纪元以来的秒数为单位给出时间。对于UNIX系统,
import os
stat = os.stat(path_to_file)
try:
print(stat.st_birthtime)
except AttributeError:
# Probably on Linux. No easy way to get creation dates here,
# so we'll settle for when its content was last modified.
print(stat.st_mtime)输出结果
这将给出输出:
1505928271.0689342
要获取文件的修改时间,可以使用os.path.getmtime(path)。支持跨平台。
示例
>>> import os
>>> print os.path.getmtime('my_file')
1505928275.3081832热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短