Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.musoftware.de!wum.musoftware.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Neil Cerutti Newsgroups: comp.lang.python Subject: Re: Accessing the files by last modified time Date: 22 Mar 2012 13:06:36 GMT Organization: Norwich University Lines: 54 Message-ID: <9t0mesFnj3U1@mid.individual.net> References: <26215939.1574.1332416026298.JavaMail.geo-discussion-forums@ynlt17> <8cbfb1b2-94ed-4830-90fa-976a831ebb97@l14g2000vbe.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: individual.net otPwBMAAyZkEEmFp5vDntgrDiGM8Z99C11EbRZp1f8TQ54I9lE Cancel-Lock: sha1:o2weVscl+shZ3iTg/evA/Qw9+c8= User-Agent: slrn/0.9.9p1/mm/ao (Win32) Xref: csiph.com comp.lang.python:22020 On 2012-03-22, Tim Williams wrote: > On Mar 22, 7:33?am, Sangeet wrote: >> Hi >> >> I am new to the python programming language. >> >> I've been trying to write a script that would access the last >> modified file in one of my directories. I'm using Win XP. >> >> I saw a similar topic, on the forum before, however the reply >> using (os.popen) didn't work out for me. I'm not sure whether >> it was due to syntax errors either. >> >> Thanks, >> Sangeet > > Check out os.stat() I've been using os.path.getmtime, and converting that to a datetime object using datetime.datetime.fromtimestamp. Surprisingly, perhaps, this has been working. According to the docs, to avoid making any assumptiong, I might need to do: tm = os.path.getmtime(apath) lt = time.localtime(tm) datetime.datetime(lt.tm_year, lt.tm_mon, lt.tm_mday) Here's where I'm confused about the functioning of my original plan. The docs say: os.path.getmtime [...] The return value is a number giving the number of seconds since the epoch (see the time module). [...] classmethod datetime.fromtimestamp Return the local date and time corresponding to the POSIX timestamp, such as returned by time.time(). [...] time.time Return the time as a floating point number expressed as seconds since the epoch, in UTC. [...] I'm not completely sure the return type of getmtime and the argument type of fromtimestamp are really the same or not. I guess I came to that conclusion some time in the past, and it does seem to work. It may be a simple case of just different aspects the exact same type being being highlighted in each definition. -- Neil Cerutti