Trying to use open(filename, 'w' ) gives IOError: [Errno 2] No such file or directory if directory doesn't exist

You are correct in surmising that the parent directory for the file must exist in order for open to succeed. The simple way to deal with this is to make a call to os.makedirs.

From the documentation:

os.makedirs(path[, mode])

Recursive directory creation function. Like mkdir(), but makes all intermediate-level directories needed to contain the leaf directory.

So your code might run something like this:

filename = ...
dirname = os.path.dirname(filename)
if not os.path.exists(dirname):
    os.makedirs(dirname)
with open(filename, 'w'):
    ...

If you try to create a file in a directory that doesn't exist, you will get that error.

You need to ensure the directory exists first. You can do that with os.makedirs() as per this answer.