date formatting

Jeremy Dillworth jwdillworth at yahoo.com
Fri Sep 12 14:37:22 EDT 2003


This works, with Python 2.3:

from datetime import datetime
datetime.fromtimestamp(os.stat('thefile').st_mtime).strftime('%y%d%m')

It leaves out the call to localtime, but now you're constructing a 
datetime object, so it's really not more direct.  Some may find it
more readable, or at least I think it is, er... well slightly :)

On Linux os.stat() returns an object of type posix.stat_result,
I wonder if this type could be enhanced so that one could do this:

os.stat('thefile').mdatetime.strftime('%y%d%m')

(mdatetime returning a datetime object)

Naturally we'd probably want os.stat('thefile').cdatetime and so 
on too...


--- WmGill <wmgill at wmgill.com> wrote:
> I want to write a script that Among other things) renames a file based on
> it's timestamp.
> I can get the date info using
> "strftime('%y%m%d',localtime(os.stat('thefile')[ST_MTIME])", but this seems
> like a long way around the block.  Is there a more direct way?
> 
> Bill
> 
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list






More information about the Python-list mailing list