2013-08-07 4 views
0

생성 날짜를 기준으로 오래된 파일을 보관하려고합니다. 데이터가 12-17-2010에서 시작하므로 기본 날짜로 설정하고 거기서부터 증가시킵니다. 다음은 내 코드입니다파일 생성 날짜 비교

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 변수가 부동 소수점 값을 가져옵니다. 기준일과 비교하기 위해 어떻게 사용할 수 있습니까?

+1

레코드의 경우, 'ctime'은 파일 생성 날짜가 아니다 ... – twalberg

+0

'count <2' ... 이것은 2 일만 비교할 것이다. – tdelaney

+0

예. 나는 작은 세트부터 시작하고있다. 코드가 작동하기 시작하면 i가 더 늘어날 것입니다. – user2501825

답변

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

'timestamp'에는 getctime에서 반환 한 값과 비교할 수있는 타임 스탬프가 포함됩니다. Windows getctime에서 UNIXes 수정 시간 (http://docs.python.org/3.1/library/os.path.html)에서 생성 시간을 반환합니다. (주석 질문에 대한)

편집 :

1) mktime은 파이썬 2.x에서에 존재 : http://docs.python.org/2/library/time.html#time.mktime

2) Get file creation time with Python on linux

EDIT2 :

은 분명히이 바보, 아래의 tdelaney가 제안한대로 진행해야합니다.

date.fromtimestamp(filecreation) 

및 타임 스탬프가 아닌 날짜를 비교하십시오. 나는 알고리즘이 실제로 무엇을하는지 보지 않았다.

+0

파이썬 2.6에도 적용 할 수 있습니까? 유닉스에서 어느 날짜에 생성 날짜를 얻으려고합니까? – user2501825

+0

답변을 업데이트했습니다. – BartoszKP

+1

나는 당신이 반대를하고 싶다고 생각한다 - ctime을 날짜 객체로 변환하라. 타임 스탬프를 ctime과 비교하면 동일한 마이크로 초 (있을 법하지 않음) 인 경우에만 일치를 얻습니다. ctime을 날짜로 변환하면 같은 날에 무엇이든 일치하게됩니다. – tdelaney