有没有一种方法可以获取使用python创建文件的年份?

有没有一种方法可以获取使用python创建文件的年份?,python,Python,标题说明了一切。我正在尝试获取为索引目的创建文件的年份。另外,如果有必要的话,我会使用windows。这个问题已经被问过了,这里是一个简短的故事,请参阅下面的代码: import os.path, time print "last modified: %s" % time.ctime(os.path.getmtime(file)) print "created: %s" % time.ctime(os.path.getctime(file)) 以下是链接: 老实说,也有类似的问题,例如 os.

标题说明了一切。我正在尝试获取为索引目的创建文件的年份。另外,如果有必要的话,我会使用windows。

这个问题已经被问过了,这里是一个简短的故事,请参阅下面的代码:

import os.path, time
print "last modified: %s" % time.ctime(os.path.getmtime(file))
print "created: %s" % time.ctime(os.path.getctime(file))
以下是链接:

老实说,也有类似的问题,例如

os.stat
是你的朋友。它在一个文件上提供各种统计信息。使用
ctime
将其更改为演示版中人类可读的内容


这就是解决方案:

import os
import datetime

#gives the create time as a unix timestamp
create_time = os.stat(<path to file>).st_ctime

#returns a datetime object
create_datetime = datetime.datetime.fromtimestamp(create_time)

#print the year
create_datetime.strftime("%Y")
导入操作系统
导入日期时间
#将创建时间作为unix时间戳提供
create_time=os.stat().st_ctime
#返回datetime对象
create\u datetime=datetime.datetime.fromtimestamp(create\u time)
#打印年份
创建\u datetime.strftime(“%Y”)

不,这不是正确的解决方案,因为Stu\ctime将返回上次更改时间。而不是创造time@RuchirShukla这就是文档定义它在unix系统上的作用——这正是我要说的。“在某些系统(如unix)上是最后一次元数据更改的时间,在其他系统(如Windows)上是创建时间(有关详细信息,请参阅平台文档)。“不,这不是正确的解决方案,因为getctime正在返回最后一次更改时间。而不是创造时间
import os
import datetime

#gives the create time as a unix timestamp
create_time = os.stat(<path to file>).st_ctime

#returns a datetime object
create_datetime = datetime.datetime.fromtimestamp(create_time)

#print the year
create_datetime.strftime("%Y")