It would be better to use the with statement if you know that you want to use this file only in your decorated function. In addition, separator is an implementation detail - I would leave it outside the shell:
def open_file(func): def a_wrapper(filename, *args): with open(filename, 'w') as f: return func(f, *args) return a_wrapper
In addition, as @Platinum Azure noted, itβs always useful to use functools.wraps for your decorator function - this will ensure that metadata of the wrapped function is present in the wrapper function (which can be useful when you want to be able to distinguish between your wrapped functions programmatically).
EDIT:
The reason you should move separator from the wrapper function is because you may have other functions that should only take the file object and not need a separator. If you create a function like this:
def say_hello(fp): fp.write("Hello World!")
You cannot use the version of the open_file decorator, which also tries to pass to separator in say_hello - try to get a TypeError because you are trying to call a function that takes one argument (file object) with two arguments - a file object and a separator.
In addition, even if all of your wrapped functions require at least one additional argument, it should not be a separator. This will be a legal function to carry even with your unedited decorator:
def laugh(fp, number_of_times): fp.write("Ha! " * number_of_times)
What I mean when I say that separator is an implementation detail. Ideally, your code is also documentation. If you will use this shell only to write data separated by a separator, then you should leave the separator argument in your wrapper, as it helps to document how the wrapper is supposed to be used. Otherwise, the second argument is no more important than the 14th, and it should not be called with its parameter.