The previous implement will cause "Permission denied" for Windows system as try to open a already opened file.

Update the code to use another method to create a tmp file, but this method need to add code to close and remove the file with:
os.close()
os.remove()