The return value of mktemp is a single string representing the filename, whereas mkstemp returns a tuple of an open OS file handle and the filename. See the docs.
A simple solution would be to replace the call like this:
-filename = tempfile.mktemp()
+filename = tempfile.mkstemp()[1]
However, that leaks the file handle in the first argument of the tuple, which needs to be closed.
A better solution might be to use TemporaryFile. However, that is also not API-compatible with mktemp.
In either case we need a more sophisticated solution.