title: Avoid Race condition
body: <p>I have a simple code that create a folder structure in a specific place.</p>
  
  <p>I am trying to have the code create and hidden <code>.okay.dat</code> file in every single folder in order to allow <code>AWS S3</code> to successfully upload everything to the cloud when I came across a race condition.</p>
  
  <p>Can someone advise the best way to avoid it?</p>
  
  <pre><code>#!/usr/bin/python
  
  print 'This script will generate the standard tree structure'
  
  while True:
          import  os
          aw_directory = "/mnt/sdb1/9999_testfolder/"
          for child in os.listdir(aw_directory):
              project_path = os.path.join(aw_directory, child)
              if os.path.isdir(project_path):
  
  #GUARD AGAINST RACE CONDITION TO BE ADDED
  #               open('001_DATALAB/.OKAY.dat', 'w').close()
  #    except OSError as exc: # Guard against race condition
  #       if exc.errno != errno.EEXIST:
  #           raise
  #with open(okaystudio.dat, "w") as f:
  #    f.write("hidden file for AWS3")
  
                  a       = os.path.join(project_path, '001_DATALAB/')
                  open('001_DATALAB/OKAYstudio.dat', 'w').close()
                  b       = os.path.join(project_path, '001_DATALAB/001_Rushes')
  
                  if not os.path.exists(a):
                      original_umask_a = os.umask(0)
                      os.makedirs(a, mode=0777)
                      os.umask(original_umask_a)
                  if not os.path.exists(b):
                      original_umask_b = os.umask(0)
                      os.makedirs(b, mode=0777)
                      os.umask(original_umask_b)
  </code></pre>