473,326 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Getting previous file name


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

Aug 7 '06 #1
7 2060
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
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
Aug 7 '06 #2
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 *
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

Aug 7 '06 #3
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
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
Aug 8 '06 #4


John Machin wrote:
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 *

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

Aug 8 '06 #5

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
Aug 8 '06 #6
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/

Aug 8 '06 #7

Miki wrote:
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))
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

Aug 8 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tetedeiench | last post by:
Hi ! I am currently changing the server for my website, and i make a heavy use of openssl functions. Both servers use PHP4.3.3 with OpenSSL 0.9.6i. I was actually testing with this code,...
7
by: Joshua Beall | last post by:
Hi All, I am doing some work where I want to do locking, and prevent scripts from running in parallel. I see that I could use the semaphore mechanism, but I'd like for my code to be portable,...
1
by: Andy Wells | last post by:
I'm using VB.NET and I have an application that binds a schema to the main form's controls, and the user has the ability to load an XML file through the schema and into the bound form. My...
1
by: Reb | last post by:
Hi all, I have not successfully found a Javascript sample for getting next and previous links from a file... I figured that someone must have solved this problem already! ... here is what I'm...
5
by: Zeba | last post by:
Hi ! I want to know the cookie value that was set in the previous page. Initially in my page in project B I used HttpCookie cookie = Request.Cookies.Get(somefile.COOKIE_NAME); where...
0
by: guilho | last post by:
Hi all! I'm newbie on php. On the web site i'm developing, i have encoutered a problem that i can't resolve. I have a script that makes the file upload, and then, based on the upload, it displays...
3
by: ITAutobot25 | last post by:
Now this is really the last problem (for real now) with this assignment. My sorter is not working. I managed to sort by product name in my previous assignment; however, I can't get it to work on this...
1
by: desivirus | last post by:
hi admin.. i followed your tip in "HOW TO LIST PROCESS ID IN WINDOWS" thread..and iam trying to compile this code in cygwin , $gcc -mno-cygwin process.c -o -L"psapi.lib" process.exe psapi.h...
185
by: jacob navia | last post by:
Hi We are rewriting the libc for the 64 bit version of lcc-win and we have added a new field in the FILE structure: char *FileName; fopen() will save the file name and an accessor function will...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.