title: Renaming a duplicate file using count (python)
body: <pre><code>#!/usr/bin/python
  import os, datetime
  import os.path
  import sys
  import time
  import shutil
  import re
  import itertools
  
  today = datetime.date.today()
  todaystr = today.isoformat()
  
  if os.path.exists('/var/log/brd/' + todaystr) is False and os.path.isfile('/var/log/brd/brd.log') is True:
      os.mkdir('/var/log/brd/' + todaystr) 
      shutil.move('/var/log/brd/brd.log', '/var/log/brd/' + todaystr)
      print "Directory Created, Moved Log File" 
      sys.exit()
  
  elif os.path.isfile('/var/log/brd/brd.log') is True and os.path.exists('/var/log/brd/'+todaystr) is True: 
      num_files = sum(1 for file in os.listdir('/var/log/brd/') if os.path.isfile('/var/log/brd/{}/brd.log'.format(todaystr,)))
      next_num = num_files + 1
      os.rename('/var/log/brd/brd.log', '/var/log/brd/' + todaystr + '/blackbird.log_{}'.format(next_num))
      print "Renaming Duplicate"
      sys.exit() 
  
  else:
      print "No Duplicate Found"
      sys.exit()
  </code></pre>
  
  <p>My situation:
  Everyday a new dir. is created. It moves the days .log file into the daily directory.
  Now, if it moves the same .log file in I need to add a count to the end of the file name (1,2,3...).  I can do this in bash rather easily using date/minutes/seconds, but I need to figure out how to use the count eventually.</p>
  
  <p>I'm pretty new to python scripting so any help is appreciated.</p>
  
  <p>Ok... here is the updated code.  I'm just trying to get everything running before I start cleaning it up...</p>
  
  <p>Now... it runs good.. moves the file.  If it is run again it adds the count to the end (ends up being 18????) then when it goes to move/rename again it just replaces the _18 file with the newer log...
  Keep in mind this is my first python script I have ever wrote. </p>
  
  <pre><code>e.@IT-105-WS4:~$ touch /var/log/brd/brd.log
  e@IT-1~$ python BBdirectory.py 
  Renaming Duplicate
  e@IT1:~$ ls /var/log/blackbird/2012-10-05
  brd.log  brd.log_19
  e@IT-1:~$
  </code></pre>
  
  <p>it repeats the above process never making a 20 21...</p>