[Tutor] Getting a directory listing with Python to MySQL

Steven D'Aprano steve at pearwood.info
Fri Jul 25 11:20:00 CEST 2014


On Tue, Jul 22, 2014 at 06:32:43PM -0700, Eric Dannewitz wrote:

> That's close. I have been playing from glob and os.walk but I'm at a 
> loss how to get the size, creation and modified date while running it.

Did you look at the functions in os.path as suggested? Here are the 
docs:  https://docs.python.org/2/library/os.path.html

Here's an example:

py> filename = '/tmp/rubbish'
py> with open(filename, 'w') as fp:
...     x = fp.write('hello world')
...
py> import os
py> os.path.getsize(filename)
11
py> os.path.getmtime(filename)
1406277914.0


To convert the timestamp into a datetime object and human-readable 
string:

py> import datetime
py> d = datetime.datetime.fromtimestamp(os.path.getmtime(filename))
py> d
datetime.datetime(2014, 7, 25, 18, 45, 14)
py>
py> d.strftime('%Y-%m-%d %H:%M:%S')
'2014-07-25 18:45:14'


By the way, "creation date" is not supported on all operating systems 
or file systems. The following is a little technical, but the executive 
summary is this:

    Python supports three file timestamps, atime, ctime and mtime.
    atime and mtime mean the same thing on every common system, but
    ctime does not: it may be creation time, or last change time.


This is where it starts getting complicated...

On Linux, Unix, Mac OS X, and other POSIX systems, three timestamps are 
normally recorded:

    atime: the last access (read or write) time
    ctime: the metadata change time, **not** creation time
    mtime: the last modification time

Metadata change time means when the file permissions or ownership 
changes. Some POSIX systems can record the birth or creation time as 
well, or the time of last backup, depending on the file system. BSD and 
recent versions of Cygwin also support btime (birth or creation time), 
but there's no easy way to get that information from Python.

On Windows, three or four timestamps are recorded, depending on the file 
system being used:

Windows using FAT file system (Windows 95, 98, most USB sticks):
    atime: the last access time
    ctime: the creation time
    mtime: the last modification time

Windows using NTFS file system (Windows NT, XP and newer):
    atime: the last access time
    ctime: the creation or birth time
    mtime: the last modification time
    plus the last metadata change time.

Only mtime is consistently recorded by all common file and operating 
systems. Although atime is also recorded, some systems allow you to turn 
it off so it is no longer updated.

In Python, you can use os.path.getatime, getctime and getmtime functions 
to return the atime (access), mtime (modification) and ctime (either 
creation or change) time.

Oh, one last thing: be aware that the different timestamps have 
different resolutions. For example, on FAT file systems (like many USB 
sticks), the access time has a resolution of a full day! So getatime() 
on a file on a USB stick will probably tell you only what *day* it was 
last accessed, not the actual time.



-- 
Steven


More information about the Tutor mailing list