title: Why do I get Python IOError: [Errno 13] Permission denied on writing to a file?
body: <p>I cannot figure out what I am doing wrong. I am running some tests and writing the results to a file. The portion of the code which writes to a file is the following (in a class called Tester):</p>
  
  <pre><code> @staticmethod
   def printHeader(resultsFileName):
          if not os.path.isfile(resultsFileName):
              # The file does not exist, thus
              # we need to print the header
  
              # Opens the results file
              with open(resultsFileName,"a") as file:
                  # Prints the header 
                  file.write("A long header")
  
  
  @staticmethod
  def printResults(resultsFileName,otherArguments):
  
      # Prints the header if it does not exist
      Tester.printHeader(resultsFileName)
  
      # Prints the results
      with open(resultsFileName,"a") as file:
  
          file.write(otherArguments)
  </code></pre>
  
  <p>Sometimes I get this error:        </p>
  
  <pre><code>Traceback (most recent call last):
    File "main.py", line 74, in &lt;module&gt;
    File "tester.py", line 88, in methodOne
    File "tester.py", line 441, in printResults
    File "tester.py", line 428, in printHeader
  IOError: [Errno 13] Permission denied: 'results.txt'
  </code></pre>
  
  <p>while other times it runs smoothly. I cannot figure out where the problem is. Any ideas?</p>
  
  <p><strong>NOTE1</strong>: I have <code>rwx</code>permission on the directory where the file is written.
  <strong>NOTE2</strong>: The error happens after several lines of results have already been written. Thus, it happens when the code is checking whether the header should be printed or not (but it is not supposed to print it, since the file exists).</p>
  
  <p><strong>UPDATE 1</strong>: </p>
  
  <p>As suggested, I have changed my code to avoid opening and closing the file multiple times. Now it writes everything in one shot. This is the updated code:</p>
  
  <pre><code>  @staticmethod
    def printResults(resultsFileName,otherArguments):
  
      # Prints the header if it does not exist                                                                                                                                                                                                                                                                                                                            
      if not os.path.exists(resultsFileName):
  
          # The file does not exist, thus                                                                                                                                                                                                                                                                                                                                 
          # we need to print the header                                                                                                                                                                                                                                                                                                                                   
  
          # Opens the results file               
          # HERE IS WHERE ERRNO 13 HAPPENS
          # STRANGELY, THE FILE DOES EXIST 
          # AS SEVERAL LINES OF RESULTS 
          # HAVE ALREADY BEEN WRITTEN                                                                                                                                                                                                                                                                           
          with open(resultsFileName,"w") as file:
  
              # Prints the header                                                                                                                                                                                                                                                                                                                                         
              file.write("A suitable header")
              # Prints the results                                                                                                                                                                                                                                                                                                                                        
              file.write(otherArguments)
      else:
  
          # Prints the results                                                                                                                                                                                                                                                                                                                                            
          with open(resultsFileName,"a") as file:
              file.write(otherArguments)   
  </code></pre>
  
  <p>It seems that <code>os.path.exists()</code> at some point returns <code>FALSE</code>even if the file does exist. Probably, there is something revoking me permission to write (perhaps the file is not properly closed after writing?). </p>
  
  <p>The explanation of <code>os.path.exists()</code> says that:</p>
  
  <blockquote>
    <p>On some platforms, this function may return False if permission is not granted to execute os.stat() on the requested file, even if the path physically exists.</p>
  </blockquote>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>I have changed my code to the following, to avoid <code>os.path.isfile()</code>:</p>
  
  <pre><code># Opens the results file                                                                                                                                                                            
  with open(resultsFileName,"a") as file:
       if file.tell() == 0:
           # Prints the header                                                                                                                                                                         
           file.write("Header")
           # Prints the results                                                                                                                                                                        
           file.write(otherArguments)
           file.close()
        else:
  
           # Prints the results
           file.write(otherArguments)                                                                                                                                                                        
           file.close()
  </code></pre>
  
  <p>Nevertheless, ERRNO 13 happens at <code>with open(resultsFileName,"a") as file:</code>.
  I have <code>rw</code>permissions both on the folder and on the file, on which several lines are written before the error happens. The OS is Linux.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  </p>