with open('./file/test.txt', 'wb+') as f: for i in nums: s = struct.pack('i', i) f.write(s)
读:
nums = [] with open('./file/2010.txt', 'rb+') as f: for i in range(len1): data = f.read(4) elem = struct.unpack('i', data)[0] nums.append(elem)
注意:struct返回的元组
def readFile(url): res = [] with open(url, 'rb+') as f: while True: data = f.read(8) if not data: break elem = struct.unpack('2i', data) res.append(elem) return res