title: Pythonic way of write if open is successful
body: <p>I am looking for a pythonic way to create and write to file if opening it was successful or else return an error if not (e.g. permission denied).  </p>
  
  <p>I was reading here <a href="https://stackoverflow.com/questions/8404856/whats-the-pythonic-way-of-conditional-variable-initialization/8405005#8405005">What&#39;s the pythonic way of conditional variable initialization?</a>. Although I'm not sure this method works as I have attempted to test it.</p>
  
  <pre><code>os.write(fd,String) if (fd = os.open(str("/var/www/file.js"),os.O_RDWR)) else return HttpResponse("error on write")
  </code></pre>
  
  <p>It's supposed to be a one-liner.</p>
  
  <p>When I do the above I get a syntax error as such:</p>
  
  <pre><code>    os.write(fd,String) if (fd = os.open(str("/var/www/file.js"),os.O_RDWR)) else return HttpResponse("error on write")
                                 ^
  SyntaxError: invalid syntax `
  </code></pre>
  
  <p>Is there a more and correct pythonic one-line or two-liner to be able to achieve this?</p>