title: Python give full permissions to read/write a file
body: <p>While working on Python(3.8.0), I am trying to create a PID file in the /var/run directory. On <strong>enter</strong>, I need to create the file and on <strong>exit</strong>, I need to delete the file. </p>
  
  <pre><code>class PIDFile(object):
      def __init__(self, filename='pidfileCreated.pid'):
          self._file = os.path.join("/var/run", filename)
  
      def __enter__(self):
  
          with open(self._file, "w") as f:
              f.write(str(os.getpid()))
              f.close()
              os.chmod(self._file, 0o777)
  
          return self
  
      def __exit__(self, *args):
          if os.path.exists(self._file):
              try:
                  os.remove(self._file)
              except OSError:
                  pass
  </code></pre>
  
  <p>But I get the error - </p>
  
  <pre><code>with open(self._file, "w") as f:
  PermissionError: [Errno 13] Permission denied: '/var/run/pidfileCreated.pid'
  </code></pre>