title: Python: open function fails to open a new file with append
body: <p>I am writing a custom <code>ansible</code> module.</p>
  
  <p>Here is how it is invoked:</p>
  
  <pre><code>  - name: mymodule.yml -&gt; Run mymodule
      mymodule:
        test_file: "{{ item }}"
        env_file: "{{ environment_file.stdout }}"
        out_dir: "{{ nginx_folder }}/{{ rv_dir_name.stdout }}"
        base_url: "{{ results_base_url }}"
      become: yes
  </code></pre>
  
  <p>At some point in time, within the module, the following python commands are invoked:</p>
  
  <pre><code># use file for the stderr descriptor of the subprocess.call
  try:
      # os.mkdir(_out_dir, 0755 )
      ferr = open('std_err.log', 'w')
      # fout = open('std_out.log', 'w')   
      fout = open(_out_dir + "/" + 'std_out.log', 'a')
  except Exception as exc:
      module.fail_json(msg="Hello: " + str(exc))
  # fout = open('std_out.log', 'a')
  </code></pre>
  
  <p>The following line gives this error when I execute the module:</p>
  
  <blockquote>
    <p>"msg": [Errno 2] No such file or directory: '/var/www/html/newman-tests/2019-03-25_09:34:04/std_out.log'",</p>
  </blockquote>
  
  <p>Why does it fail to create / open the file, despite the fact that:</p>
  
  <p>a) the module is called with <code>become: yes</code></p>
  
  <p>b) <code>open</code> is called with <code>a</code> that should create the file if it doesn't exist?</p>
  
  <p><strong>edit</strong>: the folder <strong>does</strong> exist because calling this in advance</p>
  
  <pre><code>os.mkdir(_out_dir, 0755 )
  </code></pre>
  
  <p>Fails with <code>directory exists</code></p>
  
  <p>What is more, a few lines below in my module I am calling this:</p>
  
  <pre><code>rc = call(["newman", "run", _test_file, "-k", "-e", _env_file, "-r", "htmlextra,cli", "--reporter-html-report", "--reporter-htmlextra-export", _output], stderr=ferr)
  </code></pre>
  
  <p>Where </p>
  
  <pre><code>_output = _out_dir + "/" + _report_fname
  </code></pre>
  
  <p>i.e. <code>_out_dir</code> is the same dir where the above <code>open</code> fails</p>
  
  <p>(and this works OK)</p>