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

LVL 31
James MurrellProduct SpecialistAsked:
Who is Participating?
 
peprConnect With a Mentor Commented:
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.

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

0
 
peprCommented:
The getctime() returns the number of seconds since the epoch (the fixed date in history; here 1st January 1970). To get the same time of the yesterday, you just subtract the number of seconds in one day, i.e. (24 * 3600).

If I understand the intention correctly, you want to backup all files that are somehow related to yesterday. However the script seems to be somehow confused or not finished.

Firstly, pay attention to the fact that os.path.getctime(fname) means slightly different things on UNIX (time of the last change) and on Windows (creation time). You may want to check the os.path.getmtime(fname), i.e. last modification time.

The os.path.getctime() or getmtime() returns the date related to the file (or path). Current time can be obtained by calling time.time() (i.e. function time() from the standard module with the same name).

0
 
peprCommented:
In your script, the fb was probably meant to be the "border" time, but it is set to 0. If you want to set it to the time from now to 24 hours back, just do:

import time

...

fb = time.time() - 24*3600

However, the intention of the script seems to be rather to find the name of the newest file name of the searched (here the latest) after finishing the loop. Try to put

print latest

to the end of the script.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
peprCommented:
One more suggestion. The script is simple and it possibly does not do what you want. Describe your needs. It is likely that it could be implemented also by simple script, possibly using slightly different approach.

What flaws I can see in the original script are, for example: the files could be searched/tested without the need to swith the working directory; the file extension could be extracted using higher level abstraction function thus making it less magical ;)
0
 
James MurrellProduct SpecialistAuthor Commented:
thanks, what is happening is the script is looking @ 7zip file, getting the lastest one, then extracting one file from that zipped file then ftping the file here is all code with  important stuff (network) marked out
import os
import subprocess
from ftplib import FTP
 
# config
servername = '@@@@@@@@@@@@@'
dbname = '@@@@@@@@@@@'
ftp_uname = '@@@@@@@@@@@@@@@@@@'
ftp_pwd = '@@@@@@@@@@'
ftp_server = '@@@@@@@@@@@@@@@'
# end of config
 
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).days > 1
        if (ft > fb):
            latest = ff
 
if latest != None:
    print 'Latest archive found:',latest
    ffile = dbname + '.7z'
    bfile = dbname + '.bak'
    # remove any existing backup or single db archive
    try:
        os.remove(ffile)
        print 'Removed old ',ffile
        os.remove(bfile)
        print 'Removed old ',bfile
    except:
        pass # ignore delete exceptions
    # make parameters to extract backup from archive
    params = ' e -y ' + latest + ' ' + latest[:-3] + '/' + dbname + '.bak'
    subprocess.call('7z ' + params)
    # make parames for creating new single db archive
    params = ' a ' + dbname + '.7z ' + dbname + '.bak'
    subprocess.call('7z ' + params)
    #  connect to ftp server and transfer new single db archive file
    try:
        print 'Connecting to ftp server'
        ftp = FTP(ftp_server)
        ftp.login(ftp_uname,ftp_pwd)
        tfile = open(ffile, 'rb')
        print 'Transferring:',ffile
        ftp.storbinary('STOR ' + ffile, tfile)
        ftp.quit()
        tfile.close()
        print 'Transfer complete'
    except Exception,e:
        print e
    # clean up temp files
    print 'Clean up temp files'
    os.remove(ffile)
    os.remove(dbname + '.bak')
    print 'Finished.'

Open in new window

0
 
James MurrellProduct SpecialistAuthor Commented:
mmm just got error

NameError: name 'time' is not defined
0
 
peprCommented:
Add

import time

to the beginning of the script.
0
 
James MurrellProduct SpecialistAuthor Commented:
whoops sorry missed that bit.  

thanks for all you help and explaining, will be testing this within the hour...
0
 
James MurrellProduct SpecialistAuthor Commented:
sorry for the dealy that is great thanks.......

0
 
James MurrellProduct SpecialistAuthor Commented:
A+ sir thank you
0
 
peprCommented:
You are welcome. I am just learning MS SQL 2005. This way YOU may be the target of my next questions ;)
0
 
James MurrellProduct SpecialistAuthor Commented:
LOL maybe
0
All Courses

From novice to tech pro — start learning today.