如何使用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热门推荐
1 狗狗技能训练
5 如何训练狗狗笑
6 狗狗训练排便
9 狗狗响片训练
10 如何训练狗狗不捡食
11 狗狗尿训练
12 怎么训练狗狗找东西
13 狗狗训练站立
14 狗狗训练飞盘
15 鞍山狗狗训练
16 怎样训练泰迪狗狗谢谢
17 狗狗外出训练
18 怎么训练金毛狗狗大小便