问题描述:
之前创建的文件夹,忘记了创建时间,如何通过程序获取第一次的创立时间呢?
代码实现:
import os
import platform
from datetime import datetime def get_creation_time(path):if platform.system() == 'Windows':# On Windows, use creation time if availablereturn os.path.getctime(path)else:# On Unix-like systems, use birth time if availablestat = os.stat(path)try:return stat.st_birthtimeexcept AttributeError:# Some systems may not have birth time, in that case, use last modification timereturn stat.st_mtime# 指定要查看创建时间的文件夹路径
folder_path = '/home/qtxu/UniCOQE_20230812/outputs/aste/zhijiang_unicoqe/extraction/6548_60'# 获取文件夹的创建时间
creation_time = get_creation_time(folder_path)
dt_object = datetime.fromtimestamp(creation_time)
formatted_time = dt_object.strftime("%Y-%m-%d %H:%M:%S")print(f"The creation time of the folder '{folder_path}' is: {creation_time}")
print(f"The creation time of the folder '{folder_path}' is: {formatted_time}")
运行结果: