-1

I have a list of files(Actually these are the files in some directory) as below for example

import os
path  = '/home/user/folder'
files = os.listdir(path)

so result is as below

files = ['backup_file_2010-06-30_category.zip','backup_file_2010-06-28_category.zip',
         'backup_file_2010-06-26_category.zip','backup_file_2010-06-24_category.zip',
         'backup_file_2010-06-23_category.zip','backup_file_2010-06-20_category.zip'
         'some_text_files_one.txt','some_text_files_two.txt']

so from this list i need to delete the zip files that contains the date in it on a condition that, the files that are created before five days from today needs to be deleted

I mean if the file created today is backup_file_2013-04-17_category.zip, we need to delete the files that are created before five days from today something like the files named as backup_file_2013-04-11_category.zip

Can anyone please let me know how to do this in python

Shiva Krishna Bavandla
  • 25,548
  • 75
  • 193
  • 313

3 Answers3

1

You could do something like that and in the filtered_files list you have the list of files that need to be deleted. It works if your backup files starting with prefix.

from datetime import datetime
from datetime import timedelta
import os

path  = '/home/user/folder'
files = os.listdir(path)

prefix = 'backup_file_'
days = 5

filtered_files = []
five_days_ago = datetime.now() - timedelta(days=days)
date_before = '%s%s' % (prefix, five_days_ago.strftime('%Y-%m-%d'))
for f in files:
  if f.startswith(prefix) and f < date_before:
    filtered_files.append(f)

print filtered_files
Lipis
  • 21,388
  • 20
  • 94
  • 121
0
import datetime, os
mydaterange=[datetime.datetime.today()-datetime.timedelta(days=x) for x in range(1,6)]
myfilenames=['backup_file_'+ str(x.year)+'-'+ str(x.month) + '-' + str(x.day)+ '_category.zip' for x in mydaterange]
for files in os.listdir('/home/user/folder'):
    if files.endswith('.zip') and files.startswith('backup_file_'):
        if files not in myfilenames:
            os.remove(files)
namit
  • 6,780
  • 4
  • 35
  • 41
0

You can extract the date from each filename using a regex, and compare it to see if the backup file is indeed old. If there is no match from the regex then it's not a backup file.

from datetime import date
import re

OLD_FILE_DAYS = 5

def is_old_backup_file(filename, today):
    m = re.match('backup_file_(\d\d\d\d)-(\d\d)-(\d\d)_category.zip', filename)
    if not m:
        return False
    year, month, day = (int(s) for s in m.groups())
    d = date(year, month, day)
    delta = today - d
    return delta.days > OLD_FILE_DAYS

files = ['backup_file_2010-06-30_category.zip','backup_file_2010-06-28_category.zip',
         'backup_file_2010-06-26_category.zip','backup_file_2010-06-24_category.zip',
         'backup_file_2010-06-23_category.zip','backup_file_2010-06-20_category.zip',
         'some_text_files_one.txt','some_text_files_two.txt'] # os.listdir(path)

today = date(2010, 7, 1) # date.today()

filtered_files = [f for f in files if not is_old_backup_file(f, today)]

print filtered_files

Output:

['backup_file_2010-06-30_category.zip', 'backup_file_2010-06-28_category.zip',
'backup_file_2010-06-26_category.zip', 'some_text_files_one.txt',
'some_text_files_two.txt']
Francisco Vieira
  • 196
  • 1
  • 10