title: Permission Error Writing to a File with Python Script Launched as PHP/www-data
body: <p>I have an index.php file that runs a python script as follows:</p>
  
  <pre><code>shell_exec("/usr/custom/test.py")
  </code></pre>
  
  <p>The test.py file tries to write to a file (which by default does not exist in the directory) as follows:</p>
  
  <pre><code>#!/usr/bin/env python
  
  import io
  
  with io.FileIO("foobar.txt, "w") as file:
      file.write("Hello!")
  </code></pre>
  
  <p>Now, if I run this file directly as root in Ubuntu, it works fine. The file is created and has "Hello!" inside.</p>
  
  <p>If I run it through the php script (by visiting my index.php page), I get the following error in  /var/log/apache2/error.log</p>
  
  <pre><code>Traceback (most recent call last):
    File "/usr/custom/test.py", line 5, in &lt;module&gt;
      with io.FileIO("foobar.txt", "w") as file:
  IOError: [Errno 13] Permission denied: 'foobar.txt'
  </code></pre>
  
  <p>now...I tried another python file that just prints something and I was able to get that to run fine. So here's where I'm getting confused.</p>
  
  <p>The permissions of the folder and the test.py file is the same and as follows:</p>
  
  <pre><code>-rwxrwxrwx 1 www-data www-data 102
  </code></pre>
  
  <p>I can't figure out why this won't let me write to the file. I even tried to create the file first (touch) and then give it permissions...but it didn't work.</p>
  
  <p>Does anyone know how I can get this to work?</p>