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).
I was reading here What's the pythonic way of conditional variable initialization?. Although I'm not sure this method works as I have attempted to test it.
os.write(fd,String) if (fd = os.open(str("/var/www/file.js"),os.O_RDWR)) else return HttpResponse("error on write")
It's supposed to be a one-liner.
When I do the above I get a syntax error as such:
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 `
Is there a more and correct pythonic one-line or two-liner to be able to achieve this?