Question : change os.path.getctime

i need this script which was developed by others, to look for 1 day old not todays date..
i include a snippet of the code which is calling the date

hope you guys can help
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
servername = 'DBBackup_' + servername + '_FULL'
os.chdir('e:/DBBackup')
files = os.listdir('.')
fb = 0
latest = None
# sort out latest archive from the hosting server
print 'Finding latest archive'
for ff in files:
    if (ff[-3:] == '.7z') and (ff.find(servername) != -1):
        ft = os.path.getctime(ff)
        if (ft > fb):
            latest = ff
Open in New Window Select All

Answer : change os.path.getctime

If I understand you correctly, then you want to restrict searching the lastest archive only to those older than one day. As it is the archive file, then there is no if using getctime() or getmtime(). See the minor modification (the stippet) for doing that in the for loop.

Now I also understand why doing os.chdir(). Minor enhancement here could be using also os.getcwd() first and changing to the original working directory when finishing.

You can also use the following functions for extracting the parts of the path/filename/extension:

os.path.basename(fname) ... returns the last name in the path
os.path.split(fname) ... returns both the path and the last part of the path
os.path.splitext(fname) ... returns filename without the extension plus the ".ext" extension with the dot
os.path.join(subdir, subdir, subdir, barename) .... to get rid of concatenation with OS dependent path separator

See http://docs.python.org/lib/module-os.path.html for details.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
import time
 
....
 
# sort out latest archive from the hosting server
print 'Finding latest archive'
yt = time.time() - 24*3600    # yesterday time (i.e. exactly 24 hours ago)
for ff in files:
    if (ff[-3:] == '.7z') and (ff.find(servername) != -1):
        ft = os.path.getctime(ff)
        if ft < yt and ft > fb:
            latest = ff
Open in New Window Select All
Random Solutions  
 
programming4us programming4us