Getting previous file name

H

Hitesh

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
 
L

Larry Bates

Hitesh said:
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
Just some quick ideas (not tested):

change test1.append(create_date) to test1.append((create_date, filename))
that way the filename will tag along during the sorting as a tuple in
the test1 list.

You will also need to change prev_file = walktree(path) to
create_date, prev_file = walktree(path)

Note: Your script can't handle the situation where there are zero or
one file(s) in the path (you should probably put in some code for those
edge cases).

-Larry Bates
 
J

John Machin

Hitesh said:
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 *

Lose that, and use ".st_ctime" instead of "[ST_CTIME]" below
def walktree(path):

This function name is rather misleading. The function examines only the
entries in the nominated path. If any of those entries are directories,
it doesn't examine their contents.
test1 = []
for f in os.listdir(path):
filename = os.path.join(path, f)

os.listdir() gives you directories etc as well as files. Import
os.path, and add something like this:

if not os.path.isfile(filename):
print "*** Not a file:", repr(filename)
continue
create_date_sces = os.stat(filename)[ST_CTIME]

Do you mean "secs" rather than "sces"?
create_date = time.strftime("%Y%m%d%H%M%S",
time.localtime(create_date_sces))
print create_date, " ....." , f
test1.append(create_date)

Answer to your main question: change that to
test1.append((create_date, filename))
and see what happens.
test1.sort()

If there is any chance that multiple files can be created inside 1
second, you have a problem -- even turning on float results by using
os.stat_float_times(True) (and changing "[ST_CTIME]" to ".st_ctime")
doesn't help; the Windows result appears to be no finer than 1 second
granularity. The pywin32 package may provide a solution.
print test1
return test1[-2]


if __name__ == '__main__':
path = '\\\\srv12\\c$\\backup\\my_folder\\'

(1) Use raw strings. (2) You don't need the '\' on the end.
E.g.
path = r'\\srv12\c$\backup\my_folder'
prev_file = walktree(path)
print "Previous back file is ", prev_file

Cheers,
John
 
H

Hitesh

Thank you everyone. It worked.

Here is the BETA 0.9 :)

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))

file_list.append((create_date, filename))
file_list.sort()
print file_list[-2]
return file_list[-2]


if __name__ == '__main__':
path = r'\\rad-db02-ny\c$\backup\rad_oltp'
create_date, prev_file = findfile(path)
print "Previous back file is: ", prev_file, " ", create_date

Now I am going to read this file and manupulate stuff with DB so I am
going to work on ODBC connection using python.. interesting stuff.

Thank you all,
hj
 
H

Hitesh

John said:
Hitesh said:
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 *

Lose that, and use ".st_ctime" instead of "[ST_CTIME]" below

Not sure how to do that so I am going to leave it alone.
def walktree(path):

This function name is rather misleading. The function examines only the
entries in the nominated path. If any of those entries are directories,
it doesn't examine their contents.
test1 = []
for f in os.listdir(path):
filename = os.path.join(path, f)

os.listdir() gives you directories etc as well as files. Import
os.path, and add something like this:

if not os.path.isfile(filename):
print "*** Not a file:", repr(filename)
continue

This is cool stuff. I am stuffing this inside my script.

create_date_sces = os.stat(filename)[ST_CTIME]

Do you mean "secs" rather than "sces"?

Yes I mean secs not sces.
create_date = time.strftime("%Y%m%d%H%M%S",
time.localtime(create_date_sces))
print create_date, " ....." , f
test1.append(create_date)

Answer to your main question: change that to
test1.append((create_date, filename))
and see what happens.
test1.sort()

If there is any chance that multiple files can be created inside 1
second, you have a problem -- even turning on float results by using
os.stat_float_times(True) (and changing "[ST_CTIME]" to ".st_ctime")
doesn't help; the Windows result appears to be no finer than 1 second
granularity. The pywin32 package may provide a solution.
print test1
return test1[-2]


if __name__ == '__main__':
path = '\\\\srv12\\c$\\backup\\my_folder\\'

(1) Use raw strings. (2) You don't need the '\' on the end.
E.g.
path = r'\\srv12\c$\backup\my_folder'
prev_file = walktree(path)
print "Previous back file is ", prev_file


Thank you
hj
 
H

Hitesh

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
 
M

Miki

Hello hj,
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'..

...
Remember that Python comes with "battaries included":
#!/usr/bin/env python

from os import walk
from os.path import getctime, join

def cmp_file_by_ctime(file1, file2):
return cmp(getctime(file1), getctime(file2))

def walktree(path):
file_list = []
for root, dirs, files in walk(path):
file_list += [join(root, file) for file in files]

file_list.sort(cmp_file_by_ctime)
return file_list[-2]

HTH,
Miki
http://pythonwise.blogspot.com/
 
J

John Machin

Miki said:
Hello hj,

Remember that Python comes with "battaries included":
#!/usr/bin/env python

from os import walk
from os.path import getctime, join

def cmp_file_by_ctime(file1, file2):
return cmp(getctime(file1), getctime(file2))

If there are F files to be sorted, the OP's method requires F calls to
os.stat(), and C comparisons of a create time, where C is presumably
O(F*ln(F)) and is certainly >= (F-1).

Your method requires 2*C calls to os.path.getctime(). This might be
considered a little over the top -- you could be charged with "assault
and battery" :)
def walktree(path):
file_list = []
for root, dirs, files in walk(path):
file_list += [join(root, file) for file in files]

file_list.sort(cmp_file_by_ctime)
return file_list[-2]

The OP gives every indication of wanting to examine all the files in
one directory, *NOT* walk over a directory tree.

Cheers,
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top