Q: Python Windows write HEX to BIN into file I have the following script that keeps a binary file as HEX into a variable and dumps it as binary into a file, all work ok under Linux but fails under Windows and I don't know why:
import os, os.path
from ctypes import *
import sys, binascii

current_dir = r".\\"
startup = "4d5a90000300000004000000ffff0000b800000000000000400000000000000000000000000000000000000000000000000000000000000000" # snipped, too big to have it here

def DumpStartupFile():
        startupbin=binascii.unhexlify(startup)
        o=open(current_dir+"\\startup.exe","w")
        o.write(startupbin)

if os.path.isfile(current_dir+"\\startup.exe"):
        True
else:
        DumpStartupFile()

Any idea why it fails under Windows?

A: You always want to open a file in binary mode when writing binary data:
o=open(current_dir+"\\startup.exe","wb")
o.write(startupbin)

Especially on Windows, opening a file in text modus leads to newlines being translated into platform-native values on write, but that is not desirable behaviour for binary data.
From the open() function documentation:

The default is to use text mode, which may convert '\n' characters to a platform-specific representation on writing and back on reading. Thus, when opening a binary file, you should append 'b' to the mode value to open the file in binary mode, which will improve portability. (Appending 'b' is useful even on systems that don't treat binary and text files differently, where it serves as documentation.)

