user2501825
user2501825

Reputation: 23

Comparing file creation date

I am trying to archive old files based on creation date. I have data starting from 12-17-2010 so i am setting this as base date and incrementing from there. Here is my code

import os, time, tarfile
from datetime import datetime, date, timedelta
import datetime

path = "/home/appins/.scripts/test/"
count = 0
set_date = '2010-12-17'
date = datetime.datetime.strptime(set_date, '%Y-%m-%d')

while (count < 2):
    date += datetime.timedelta(days=1)
    tar_file = "nas_archive_"+date.strftime('%m-%d-%y')+".tgz"
    log_file = "archive_log_"+date.strftime('%m-%d-%y')
    fcount = 0
    f = open(log_file,'ab+')
    #print date.strftime('%m-%d-%y')
    for root, subFolders, files in os.walk(path):
        for file in files:
            file = os.path.join(root,file)
            file = os.path.join(path, file)
            filecreation = os.path.getctime(file)
            print datetime.fromtimestamp(filecreation)," File Creation Date"
            print date.strftime('%m-%d-%y')," Base Date"
            if filecreation == date:
                tar.add(file)
                f.write(file + '\n')
                print file," is of matching date"
                fcount = fcount + 1
    f.close()
    count += 1

filecreation variable is getting float value. How can I use it to compare with my base date?

Upvotes: 0

Views: 2074

Answers (1)

BartoszKP
BartoszKP

Reputation: 35891

timestamp = datetime.mktime(date.timetuple())

The 'timestamp' will contain a timestamp comparable to values returned by getctime. Regarding the comment under the question: on Windows getctime returns creation time, on UNIXes modification time (http://docs.python.org/3.1/library/os.path.html).

EDIT (regarding questions in comment):

1) mktime is present in Python 2.x: http://docs.python.org/2/library/time.html#time.mktime

2) Get file creation time with Python on linux

EDIT2:

Obviously this is stupid, and one should proceed as suggested by tdelaney below:

date.fromtimestamp(filecreation)

and compare dates, not timestamps. I wasn't looking at what the algorithm was actually doing :)

Upvotes: 1

Related Questions