简体   繁体   English

在Windows中以Python编写文件并回车

[英]Writing files in Python and carriage return in Windows

I'm using OpenCV Python library to extract descriptors and write them to file. 我正在使用OpenCV Python库提取描述符并将其写入文件。 Each descriptor is 32 bytes and I only save 80 of them. 每个描述符都是32个字节,我只保存了80 字节。 Meaning that, the final file must be exactly 2560 bytes. 这意味着,最终文件必须恰好为2560字节。 But it's 2571 bytes. 但这是2571字节。

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. 我还有另一个文件是使用相同的Python脚本编写的(不是在Windows上,但我猜是在Linux上),它的大小恰好是2560字节。

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. 使用WinMerge,我试图将它们进行比较,这给了我一个警告,说两个文件中的回车符是不同的,并问我是否要一视同仁。 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? 我想知道Python是否有写二进制文件在Windows和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 . 是的,有一种方法可以以二进制模式打开文件-只需将b字符放入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' . 如果在Windows中不这样做,则每个换行符'\\n'都将转换为Windows使用的两个字符的行尾序列'\\r\\n'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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