python question
Christopher Schmidt
crschmidt at crschmidt.net
Sat Apr 16 06:33:01 EDT 2005
On Fri, Apr 15, 2005 at 10:06:46PM -0400, Paul Lussier wrote:
>
> Hi all,
>
> If I use os.path.getmtime("foo"), how do I convert that into into
> something useful? I've tried using
> date.fromtimestamp(os.path.getmtime("foo")), but that seems to not
> work for some reason:
>
> >>> import os
> >>> import date
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> ImportError: No module named date
This is telling you there's no such thing as "date".
> >>> import time
> >>> date.fromtimestamp(os.path.getmtime("/Users/pll/.emacs"))
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> NameError: name 'date' is not defined
Since there's no such thing as "date", you can't use it.
> >>> os.date.fromtimestamp(os.path.getmtime("/Users/pll/.emacs"))
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> AttributeError: 'module' object has no attribute 'date'
Nope, OS doesn't have a "date" either.
> >>> time.date.fromtimestamp(os.path.getmtime("/Users/pll/.emacs"))
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> AttributeError: 'module' object has no attribute 'date'
Getting closer...
Since you're working with time, the best place to start is with (from
the command line), `pydoc time`, which would tell you:
ctime(...)
ctime(seconds) -> string
Convert a time in seconds since the Epoch to a string in local time.
So, the correct way to do it is:
import time
time.ctime(os.path.getmtime("yahoo.py")
> I'm trying get the mtime of a file in order to compare that to a
> timestamp stored in a file in ctime(3) format (e.g., "Thu Oct 13
> 04:54:34 1994"). So I either need to convert the timestamp in the
> file from ctime to the format I get from python's os.path.getmtime(),
> or convert that to the ctime string format. Any ideas?
Hey, look, I figured out what you wanted before you even asked! :)
--
Christopher Schmidt
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mail.gnhlug.org/mailman/private/gnhlug-discuss/attachments/20050416/b5f62c33/attachment.bin
More information about the gnhlug-discuss
mailing list