Search code examples
python-3.xcopybinaryfiles

why an exact copy with "open" doesn't produce a valid binary file?


I noticed that an exact copy of an *.exe file does not quite work as expected opening the file as binary:

with open("input.exe", "rb") as f1:
    bs = f1.read()
    with open("output.exe", "wb") as f2:
        # Copy byte by byte
        f2.write(bs)

The resulting "output.exe" crashes at startup. I know that I'm being quite vague here, but from a theoretical point of view, am I missing something? I even checked and the md5 checksum of both files is the same, but still "input.exe" runs whereas "output.exe" does not.

P.S. at the beginning I wanted to encode "input.exe" as a txt file and the decoding it back to a binary file, but in the end the issue boils down to the code above.

P.S That same exact code works for other files type, like images (*.jpg)


Solution

  • I found the problem, the output file name should have had a particular name.