def copy(if_path, of_path): with open(if_path, 'rb') as in_file, open(of_path, 'wb') as out_file: while True: chunk = in_file.read(1024) if chunk: out_file.write(chunk) out_file.flush() else: break
def copy(if_path, of_path): with open(if_path, 'rb') as in_file, open(of_path, 'wb') as out_file: while True: chunk = in_file.read(1024) if chunk: out_file.write(chunk) out_file.flush() else: break
import struct buffer = bytearray(8) buffer[0]=1 buffer[1]=1 struct.pack_into('>ih', buffer, 2, 100, 200) # to convert the array to str print(bytes(buffer))
import struct def int_to_bytes(value, size, big_endian=False): bs = [] for i in range(size): offset = i * 8 b = chr((value & (0xff << offset)) >> offset) if big_endian: bs.insert(0, b) else: bs.append(b) return b''.join(bs) def int32_to_bytes(value, big_endian=False): return int_to_bytes(value, 4, big_endian) def int64_to_bytes(value, big_endian=False): return int_to_bytes(value, 8, big_endian) if __name__ == '__main__': # the two commands below do the same print(repr(int64_to_bytes(65536, big_endian=True))) print(repr(struct.pack('>Q', 65536)))
':'.join(x.encode('hex') for x in 'Hello World!')
>>> ':'.join(x.encode('hex') for x in 'Hello World!') '48:65:6c:6c:6f:20:57:6f:72:6c:64:21'
b''.join(['abc', '\x01\x02\x03', 'efg'])
>>> b''.join(['abc', '\x01\x02\x03', 'efg']) 'abc\x01\x02\x03efg'