Getting previous file name

Hitesh hitesh287 at gmail.com
Tue Aug 8 14:54:56 EDT 2006


Thank you all.
Here is my BETA ver.

import os, time, sys
from stat import *

def findfile(path):
    file_list = []
    for f in os.listdir(path):
        filename = os.path.join(path, f)
        if not os.path.isfile(filename):
            print "*** Not a file:", repr(filename)
            continue
        create_date_secs = os.stat(filename)[ST_CTIME]
        create_date = time.strftime("%Y%m%d%H%M%S",
time.localtime(create_date_secs))
        #print create_date, " ....." , f
        file_list.append((create_date, filename))
    file_list.sort()
    print file_list[-2]
    return file_list[-2]


if __name__ == '__main__':
	path = r'srv12\\c$\\backup\\my_folder'
	create_date, prev_file = findfile(path)


Thank you
hj


Hitesh wrote:
> Hi,
>
> 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.
> Any hints... I am newbiw python dude and still trying to figure out lot
> of 'stuff'..
>
>
> import os, time, sys
> from stat import *
>
> def walktree(path):
>     test1 = []
>     for f in os.listdir(path):
>             filename = os.path.join(path, f)
>             create_date_sces = os.stat(filename)[ST_CTIME]
>             create_date = time.strftime("%Y%m%d%H%M%S",
> time.localtime(create_date_sces))
>             print create_date, " ....." , f
>             test1.append(create_date)
>     test1.sort()
>     print test1
>     return test1[-2]
>
>
> if __name__ == '__main__':
> 	path = '\\\\srv12\\c$\\backup\\my_folder\\'
> 	prev_file = walktree(path)
> 	print "Previous back file is ", prev_file
> 
> 
> Thank you,
> hj




More information about the Python-list mailing list