您好我想从使用python的Windows上的文件中获取文件ID。当我搜索时,我只能找到如何用其他语言进行搜索。有谁知道如何在python中实现这一点?
据我所研究和研究,没有这样的文件ID可用。但相反,您可以在Windows和Mac上创建创建日期,并在Linux上进行最后修改。这两个通常足以找到唯一的文件,即使它们被重命名,更改或其他任何文件。
这是如何做到的,以及source SO thread I found the solution。
import os
import platform
def creation_date(path_to_file):
"""
Try to get the date that a file was created, falling back to when it was
last modified if that isn't possible.
See http://stackoverflow.com/a/39501288/1709587 for explanation.
"""
if platform.system() == 'Windows':
return os.path.getctime(path_to_file)
else:
stat = os.stat(path_to_file)
try:
return stat.st_birthtime
except AttributeError:
# We're probably on Linux. No easy way to get creation dates here,
# so we'll settle for when its content was last modified.
return stat.st_mtime