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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.