Mikael S.
Mikael S.

Reputation: 1015

Writing files in Python and carriage return in Windows

I'm using OpenCV Python library to extract descriptors and write them to file. Each descriptor is 32 bytes and I only save 80 of them. Meaning that, the final file must be exactly 2560 bytes. But it's 2571 bytes.

I also have another file which had been written using the same Python script (Not on Windows but I guess it was on Linux) and it's exactly 2560 bytes.

Using WinMerge, I tried to compare them and it gave me a warning that the carriage return is different in two files and asked me if I wanted to treat them equally. If I say "yes", then both files are identical but if I say "no" then they are different.

I was wondering if there is anyway in Python to write binary files which produce identical result on both Windows and Linux?

Not to mention this is the relevant part of the script:

f = open("something", "w+")
f.write(descriptors)
f.close() 

Upvotes: 0

Views: 1204

Answers (1)

Mark Ransom
Mark Ransom

Reputation: 308432

Yes, there's a way to open a file in binary mode - just put the b character into the open.

f = open("something", "wb+")

If you don't do that in Windows, every linefeed '\n' will be converted to the two-character line ending sequence that is used by Windows, '\r\n'.

Upvotes: 1

Related Questions