简体   繁体   中英

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() 

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' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM