如何在Python中创建文件和修改日期/时间


如何在Python中创建文件和修改日期/时间?

以跨平台的方式获得某种修改日期很简单 - 只需调用,您将获得上次修改文件时的Unix时间戳。os.path.getmtime(path)path

跨平台代码应该看起来像这样......

import os
import platform

def creation_date(path_to_file):

    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