title: Python get files from N days ago and before
body: <p>I am trying to write a function that can find files from a certain date and before and delete them. I was playing around with fabric and I want to delete my old log files from my server. the folder has files in the following format:</p>
  
  <pre><code>['user-2015-10-16.log.gz', 'user-2015-10-19.log.gz', 'user-2015-10-22.log.gz', 'user-2015-10-25.log.gz', 'admin-2015-10-17.log.gz', 'admin-2015-10-20.log.gz', 'admin-2015-10-23.log.gz', 'requests.log', 'user-2015-10-17.log.gz', 'user-2015-10-20.log.gz', 'user-2015-10-23.log.gz', 'extra.log', 'admin-2015-10-18.log.gz', 'admin-2015-10-21.log.gz', 'admin-2015-10-24.log.gz', 'user-2015-10-18.log.gz', 'user-2015-10-21.log.gz', 'user-2015-10-24.log.gz', 'admin-2015-10-16.log.gz', 'admin-2015-10-19.log.gz', 'admin-2015-10-22.log.gz', 'admin-2015-10-25.log.gz']
  </code></pre>
  
  <p>What I want to do is keep files from today till 4 days back i.e. keep the ones from <code>25th, 24th, 23rd, 22nd</code> and delete the rest (keep extra.log and requests.log). </p>
  
  <p>I tried this:</p>
  
  <pre><code>import datetime
  
  days = 4
  
  user = []
  admin = []
  
  for i in range(days):
      that_date = datetime.datetime.now() - datetime.timedelta(days=i)
      use = 'user-{}.log.gz'.format(that_date)
      adm = 'admin-{}.log.gz'.format(that_date)
      # user.append(user)
      # admin.append(admin)
      print user, adm 
  </code></pre>
  
  <p>But realized embarrassingly late that this gives me the files I want to keep and not the ones I want to delete.</p>
  
  <p>Any help will be greatly appreciated</p>
  
  <p>Edit: if not already clear, the files are generated daily with the <code>user-(todays date)</code> format so cant hardcode anything.</p>