How to get file creation & modification date/times in Python?

前端 未结 13 1997
抹茶落季
抹茶落季 2020-11-21 11:44

I have a script that needs to do some stuff based on file creation & modification dates but has to run on Linux & Windows.

相关标签:
13条回答
  • 2020-11-21 12:13
    import os, time, datetime
    
    file = "somefile.txt"
    print(file)
    
    print("Modified")
    print(os.stat(file)[-2])
    print(os.stat(file).st_mtime)
    print(os.path.getmtime(file))
    
    print()
    
    print("Created")
    print(os.stat(file)[-1])
    print(os.stat(file).st_ctime)
    print(os.path.getctime(file))
    
    print()
    
    modified = os.path.getmtime(file)
    print("Date modified: "+time.ctime(modified))
    print("Date modified:",datetime.datetime.fromtimestamp(modified))
    year,month,day,hour,minute,second=time.localtime(modified)[:-3]
    print("Date modified: %02d/%02d/%d %02d:%02d:%02d"%(day,month,year,hour,minute,second))
    
    print()
    
    created = os.path.getctime(file)
    print("Date created: "+time.ctime(created))
    print("Date created:",datetime.datetime.fromtimestamp(created))
    year,month,day,hour,minute,second=time.localtime(created)[:-3]
    print("Date created: %02d/%02d/%d %02d:%02d:%02d"%(day,month,year,hour,minute,second))
    

    prints

    somefile.txt
    Modified
    1429613446
    1429613446.0
    1429613446.0
    
    Created
    1517491049
    1517491049.28306
    1517491049.28306
    
    Date modified: Tue Apr 21 11:50:46 2015
    Date modified: 2015-04-21 11:50:46
    Date modified: 21/04/2015 11:50:46
    
    Date created: Thu Feb  1 13:17:29 2018
    Date created: 2018-02-01 13:17:29.283060
    Date created: 01/02/2018 13:17:29
    
    0 讨论(0)
  • 2020-11-21 12:14

    os.stat https://docs.python.org/2/library/stat.html#module-stat

    edit: In newer code you should probably use os.path.getmtime() (thanks Christian Oudard)
    but note that it returns a floating point value of time_t with fraction seconds (if your OS supports it)

    0 讨论(0)
  • 2020-11-21 12:14

    There are two methods to get the mod time, os.path.getmtime() or os.stat(), but the ctime is not reliable cross-platform (see below).

    os.path.getmtime()

    getmtime(path)
    Return the time of last modification of path. The return value is a number giving the number of seconds since the epoch (see the time module). Raise os.error if the file does not exist or is inaccessible. New in version 1.5.2. Changed in version 2.3: If os.stat_float_times() returns True, the result is a floating point number.

    os.stat()

    stat(path)
    Perform a stat() system call on the given path. The return value is an object whose attributes correspond to the members of the stat structure, namely: st_mode (protection bits), st_ino (inode number), st_dev (device), st_nlink (number of hard links), st_uid (user ID of owner), st_gid (group ID of owner), st_size (size of file, in bytes), st_atime (time of most recent access), st_mtime (time of most recent content modification), st_ctime (platform dependent; time of most recent metadata change on Unix, or the time of creation on Windows):

    >>> import os
    >>> statinfo = os.stat('somefile.txt')
    >>> statinfo
    (33188, 422511L, 769L, 1, 1032, 100, 926L, 1105022698,1105022732, 1105022732)
    >>> statinfo.st_size
    926L
    >>> 
    

    In the above example you would use statinfo.st_mtime or statinfo.st_ctime to get the mtime and ctime, respectively.

    0 讨论(0)
  • 2020-11-21 12:15

    os.stat returns a named tuple with st_mtime and st_ctime attributes. The modification time is st_mtime on both platforms; unfortunately, on Windows, ctime means "creation time", whereas on POSIX it means "change time". I'm not aware of any way to get the creation time on POSIX platforms.

    0 讨论(0)
  • 2020-11-21 12:15

    I was able to get creation time on posix by running the system's stat command and parsing the output.

    commands.getoutput('stat FILENAME').split('\"')[7]
    

    Running stat outside of python from Terminal (OS X) returned:

    805306374 3382786932 -rwx------ 1 km staff 0 1098083 "Aug 29 12:02:05 2013" "Aug 29 12:02:05 2013" "Aug 29 12:02:20 2013" "Aug 27 12:35:28 2013" 61440 2150 0 testfile.txt
    

    ... where the fourth datetime is the file creation (rather than ctime change time as other comments noted).

    0 讨论(0)
  • 2020-11-21 12:18

    It may worth taking a look at the crtime library which implements cross-platform access to the file creation time.

    from crtime import get_crtimes_in_dir
    
    for fname, date in get_crtimes_in_dir(".", raise_on_error=True, as_epoch=False):
        print(fname, date)
        # file_a.py Mon Mar 18 20:51:18 CET 2019
    
    0 讨论(0)
提交回复
热议问题