将字节写入 python 中的文件
writing bytes to file in python
我正在写比特流客户端。它可以从同行那里下载片段,但我不能让它正确地将片段写入文件。问题是编码。由于编码错误,客户端将错误的字节写入文件。我找到了名为 "unicode_internal" 的编码。这似乎是正确的,但问题并没有消失。尽管文件大小不变(16384 字节),但有时文件大小会增加 16386 左右。
以下是我如何将片段写入文件。没什么特别的。
with open(path, 'a', encoding='unicode_internal') as f:
f.seek(offset, 0)
f.write(data.decode('unicode_internal'))
我尝试以 'rb' 模式打开文件,但没有用。
来自工作客户端的部分标准输出:
piece size: 16384
sum of pieces lengths: 49152
filesize: 49152
piece size: 16384
sum of pieces lengths: 65536
filesize: 65536
piece size: 16384
sum of pieces lengths: 81920
filesize: 81922 #Here it is. Size increased by 16386 bytes. The piece size is 16384
piece size: 16384
sum of pieces lengths: 98304
filesize: 98306
我做错了什么?
您需要在写入时以二进制模式打开文件bytes
:
data = bytes(...) # some data in bytes type
with open(path, 'ab') as f:
f.seek(offset, 0)
f.write(data)
在文本模式下打开时,与使用的编码无关,Python 可以进行换行转换。例如。在 Windows 上,它将单个换行符 \n
(0x0A
) 转换为 "Windows-style line-ending":\r\n
(0x0D
, 0x0A
) — 两个字符。
我正在写比特流客户端。它可以从同行那里下载片段,但我不能让它正确地将片段写入文件。问题是编码。由于编码错误,客户端将错误的字节写入文件。我找到了名为 "unicode_internal" 的编码。这似乎是正确的,但问题并没有消失。尽管文件大小不变(16384 字节),但有时文件大小会增加 16386 左右。 以下是我如何将片段写入文件。没什么特别的。
with open(path, 'a', encoding='unicode_internal') as f:
f.seek(offset, 0)
f.write(data.decode('unicode_internal'))
我尝试以 'rb' 模式打开文件,但没有用。 来自工作客户端的部分标准输出:
piece size: 16384
sum of pieces lengths: 49152
filesize: 49152
piece size: 16384
sum of pieces lengths: 65536
filesize: 65536
piece size: 16384
sum of pieces lengths: 81920
filesize: 81922 #Here it is. Size increased by 16386 bytes. The piece size is 16384
piece size: 16384
sum of pieces lengths: 98304
filesize: 98306
我做错了什么?
您需要在写入时以二进制模式打开文件bytes
:
data = bytes(...) # some data in bytes type
with open(path, 'ab') as f:
f.seek(offset, 0)
f.write(data)
在文本模式下打开时,与使用的编码无关,Python 可以进行换行转换。例如。在 Windows 上,它将单个换行符 \n
(0x0A
) 转换为 "Windows-style line-ending":\r\n
(0x0D
, 0x0A
) — 两个字符。