Getting previous file name

Tim Williams listserver at tdw.net
Mon Aug 7 17:59:10 EDT 2006


On 7 Aug 2006 13:52:16 -0700, Hitesh <hitesh287 at gmail.com> wrote:
>
> I have a small script here that goes to inside dir and sorts the file
> by create date. I can return the create date but I don't know how to
> find the name of that file...
> I need file that is not latest but was created before the last file.

I notice that your path is UNC & windows,   if you are running this on
a windows platform only you could let the windows DIR do the work for
you.

>>> import os
>>> i,o,e = os.popen3('dir /O-D /A-D /B')
>>> o.readlines()[1].strip()
'dnscheck.html'

I would put a try:except around the o.readlines()....   bit in case
there is only 0 or 1 file in the directory.

Hints:

c:\> dir /?  (enter)

>>> i,o,e = os.popen3('dir /O-D /A-D /B)
>>>o.readlines()
['recover.tbl\n', 'dnscheck.html\n', 'v3changes.txt\n',
'V3todo.txt\n', 'fupdates.pyc\n', 'Auth_db.pyc\n']

HTH :)



More information about the Python-list mailing list