title: Write file if file does not exist in a recursive way
body: <p>I am trying to check if 3 files exist in a directory in a recursive way. If one (or all of the files) does not exist, it will write a file in the respective directory.</p>
  
  <p>I found some posts that explian how to do it, as for example here <a href="https://stackoverflow.com/questions/13571134/python-how-to-recursively-go-through-all-subdirectories-and-read-files">How to recursively go through all subdirectories and read files?</a></p>
  
  <p>However, the files I want to write should only be written in certain directories.
  Imagine a diretory structure with three levels. And I want to check and write the files only in the third level.</p>
  
  <p>I tried the following code:</p>
  
  <pre><code>name1 = 'Info_1_250.csv'
  name2 = 'Info_1_500.csv'
  name3 = 'Info_1_1000.csv'
  
  path_name = '/Users/user/Desktop/test/'
  
  for root, _, files in os.walk(path_name):
  
          if name1 not in files:
  
              write_file(os.path.join(root, name1))
  
          elif name2 not in files:
  
              write_file(os.path.join(root, name2))
  
          elif name3 not in files:
  
              write_file(os.path.join(root, name1))
  </code></pre>
  
  <p>write_file is a function that I wrote that will write the file.</p>
  
  <p>The problem is that this code write the file "name1" in all directories if it does not exist. It even writes in the top level and 2nd level directories (which I don't want). However in the top level and 2nd level, it only writes the file "name1". The other two files, even though it does exist, it is not written. </p>
  
  <p>Finally, if the three files are missing from a specific directory in level3, all three files should be written. I only would like files to be written in level3.</p>
  
  <p>In other words, I want to turn the following tree:</p>
  
  <pre><code>/a
  /a/b
  /a/b/c
  /a/b/d
  /a/e
  /a/e/f
  /a/e/f/Info_1_250.csv
  /a/e/g
  /a/e/g/Info_1_500.csv
  /h
  /h/i
  /h/i/j
  /h/i/j/Info_1_500.csv
  /h/i/j/Info_1_1000.csv
  </code></pre>
  
  <p>Into the following:</p>
  
  <pre><code>/a
  /a/b
  /a/b/c
  /a/b/c/Info_1_250.csv
  /a/b/c/Info_1_500.csv
  /a/b/c/Info_1_1000.csv
  /a/b/d
  /a/b/d/Info_1_250.csv
  /a/b/d/Info_1_500.csv
  /a/b/d/Info_1_1000.csv
  /a/e
  /a/e/f
  /a/e/f/Info_1_250.csv
  /a/e/f/Info_1_500.csv
  /a/e/f/Info_1_1000.csv
  /a/e/g
  /a/e/g/Info_1_250.csv
  /a/e/g/Info_1_500.csv
  /a/e/g/Info_1_1000.csv
  /h
  /h/i
  /h/i/j/Info_1_250.csv
  /h/i/j/Info_1_500.csv
  /h/i/j/Info_1_1000.csv
  </code></pre>
  
  <p>As you can see, for every third-level directory in the tree, the files <code>Info_1_250.csv</code>, <code>Info_1_500.csv</code> and <code>Info_1_1000.csv</code> must be created when missing. The other directories must be left untouched.</p>