简体   繁体   English

在python中将字节写入文件

[英]writing bytes to file in python

I am writing bittorrent client. 我正在写bittorrent客户端。 It can download pieces from peers, but I can't make it write pieces to files correctly. 它可以从同行下载片段,但我无法正确地将文件写入文件。 The problem is the encoding. 问题是编码。 Because of the wrong encoding client is writing wrong bytes to file. 由于错误的编码,客户端正在将错误的字节写入文件。 I have found encoding called "unicode_internal". 我找到了名为“unicode_internal”的编码。 It seems to be correct one but the problem didn't go away. 这似乎是正确的,但问题并没有消失。 Despite the constant piece size(16384 bytes) sometimes the file size increases by 16386 or so. 尽管片段大小不变(16384字节),但有时文件大小会增加16386左右。 Here's how I write pieces to file. 这是我写文件的方式。 Nothing special. 没什么特别的。

with open(path, 'a', encoding='unicode_internal') as f:
    f.seek(offset, 0)
    f.write(data.decode('unicode_internal'))

I tryed to open file in 'rb' mode but it doesn't help. 我尝试在'rb'模式下打开文件,但它没有帮助。 Part of the stdout from working client: 来自工作客户的stdout的一部分:

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

What am I doing wrong? 我究竟做错了什么?

You need to open file in binary mode at write bytes : 您需要以写入bytes二进制模式打开文件:

data = bytes(...) # some data in bytes type
with open(path, 'ab') as f:
    f.seek(offset, 0)
    f.write(data)

When opening in text mode, independently of used encoding, Python can do transformations with line-ending. 在文本模式下打开时,独立于使用的编码,Python可以使用行结束进行转换。 Eg on Windows it will convert single line-feed character \\n ( 0x0A ) to "Windows-style line-ending": \\r\\n ( 0x0D , 0x0A ) — two characters. 例如,在Windows上,它会将单换行字符\\n0x0A )转换为“Windows样式的行尾”: \\r\\n0x0D0x0A ) - 两个字符。

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

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