我写了一个bittorrent客户端并成功下载 所有文件的所有部分。
在我写文件(mode='wr'
)之前和之后,我打印出文件名,piece_index,写入的字节数,以及写入字节的文件中的偏移量。写完所有文件后,我关闭文件。
但是,当我查看磁盘时,只写了第一部分。该部分完全写入文件0和文件1的开头字节。即使打印语句显示所有剩余部分都写入文件1,文件1也没有。 file.seek,file.write中没有错误。这是一些输出:
-- first piece --
offset: 0 piece_index: 0
about to write file 0: offset 0 start 0 nbytes 291
Distributed by Mininova.txt
just wrote 291 bytes at offset 0
about to write file 1: offset 0 start 291 nbytes 1048285
DF self-extracting archive.exe
just wrote 1048285 bytes at offset 0
-- next piece --
offset: 1048285 piece_index: 1
about to write file 1: offset 1048285 start 1048576 nbytes 1048576
DF self-extracting archive.exe
just wrote 1048576 bytes at offset 1048285
-- next piece --
offset: 2096861 piece_index: 2 file_index: 1
about to write file 1: offset 2096861 start 2097152 nbytes 1048576
DF self-extracting archive.exe
just wrote 1048576 bytes at offset 2096861
代码:
def _write(self, fd, offset, start, num_bytes, row):
print(fd.name[-30:])
fd.seek(offset)
fd.write(self.buffer[row][start:start+num_bytes].tobytes())
fd.seek(0)
print('just wrote {} bytes at offset {}\n'.format(num_bytes, offset))
答案 0 :(得分:2)
这应该做:
@bindable({
name:'myProperty', //name of the property on the class
attribute:'my-property', //name of the attribute in HTML
changeHandler:'myPropertyChanged', //name of the method to invoke when the property changes
defaultBindingMode: ONE_WAY, //default binding mode used with the .bind command
defaultValue: undefined //default value of the property, if not bound or set in HTML
})
def _write(self, fd, offset, start, num_bytes, row):
print(fd.name[-30:])
fd.seek(offset)
bytes_ = self.buffer[row][start:start+num_bytes].tobytes()
fd.write(bytes_)
fd.flush()
fd.seek(0)
print('just wrote {} bytes at offset {}\n'.format(len(bytes_), offset))
len(bytes_)
。