Opens a file for writing creating the folder hierarchy if required.