Using datetime in order to name files I have a script that makes use of datetime in order to name files.

Only business days (Mon - Fri) are used.

Here is my part of the code that finds the last business day:

lastBusDay = datetime.datetime.today()
shift = datetime.timedelta(max(1,(lastBusDay.weekday() + 6) % 7 - 3))
lastBusDay = lastBusDay - shift
yesterday = lastBusDay.strftime('%d%m%y')


These variables are outside of any function and are the first part of my script after the imports. I've heard that global variables are bad, and I understand why.

But does this fall under the same category?

Only the yesterday variable is used later on in the script and is used to specify the new name in an the os.renames function.
I think you only want to calculate the last working day from today. Here is a better/pythonic approach:

from datetime import timedelta, date

def lastBzDay( d = None ):
  r, d = xrange( 1, 5 ), d or date.today()
  def retfn( curDay = d ):
    w = curDay.weekday()
    prevBzDay = curDay - timedelta( days= 1 if w in r else (w + 3) % 7 )
    return prevBzDay.strftime('%d%m%y')
  return retfn
lastBzDay = lastBzDay()


Now, all you have to do is:

yesterday = lastBzDay()


and you'll get the desired result. Using a closure also gives you the ability to find the last weekday from any other dates:

print lastBzDay( date(2014, 9, 7) )  # will give you 050914


You can see a demo on codepad here.