我正在做一个这样的拉链功能:
open_file = open(file_path,'rb')
open_save = open(save_path,'wb+')
try:
open_read = open_file.read()
data = zip_data(open_read)
head_xis = b'XIS'
head_version = bytes(0)
head_type1 = bytes(1)
head_type2 = bytes(1)
head_en_type = bytes(0)
head_com_type = bytes(1)
head_sour_len = len(open_read)
# try:
md5 = hashlib.md5()
md5.update(open_read)
head_md5 = md5.digest()
print("byteeeeee")
# except:
# head_md5 = None
randoms = str(uuid.uuid1()).split('-')[0]
head_random = bytes(randoms,encoding = "utf-8")
print(head_md5)
print(head_random)
head_resour_len= len(data)
print(type(head_xis))
print(type(head_version))
print(type(head_type1))
print(type(head_type2))
print(type(head_en_type))
print(type(head_com_type))
print(type(head_sour_len))
print(type(head_md5))
print(type(head_random))
print(type(head_resour_len))
head = struct.pack('3sBBBBBI16s8sI',
head_xis,
head_version,
head_type1,
head_type2,
head_en_type,
head_com_type,
head_sour_len,
head_md5,
head_random,
head_resour_len
)
open_save.write(head)
# except Exception as e:
# print("eeeee" + str(e))
# return False,str(e)
# else:
# open_save.write(data)
# return True,''
finally:
open_file.close()
open_save.close()
它显示异常并打印如下:
byteeeeee
b'\xf9\xf4\xf2\xcb\xbfM\x11\xb5\xeeNP/\x02H\xebK'
b'f9f33502'
class 'bytes'
class 'bytes'
class 'bytes'
class 'bytes'
class 'bytes'
class 'bytes'
class 'int'
class 'bytes'
class 'bytes'
class 'int'
Traceback (most recent call last):
File "entrance.py", line 52, in startProcess
Helper.processCombine(self.selectedVer,False)
File "/Users/mapzchen/myShell/qtPro/fastColuaco/Helper.py", line 86, in processCombine
itemConf.get(version,suffix),True,True)
File "/Users/mapzchen/myShell/qtPro/fastColuaco/coluaco.py", line 514, in process
compress(build_dir,zip_dir,filter_file)
File "/Users/mapzchen/myShell/qtPro/fastColuaco/coluaco.py", line 400, in compress
zfile = zip_file(src_file_path,save_path)
File "/Users/mapzchen/myShell/qtPro/fastColuaco/coluaco.py", line 131, in zip_file
head_resour_len
struct.error: required argument is not an integer
我试图打印参数类型, 它似乎正确适合3sBBBBBI16s8sI 我对执行此异常的arg感到困惑
答案 0 :(得分:0)
在结构格式化字符串“B”中,对于字节需要变量类型'int',这是主要问题。因此,而不是 head_type1 = bytes(1)使用 head_type1 = 1
下面给出的代码给出了同样的错误。
问题:
n = bytes(2)
x = struct.pack("B", n)
解决方案:
n = 2
x = struct.pack("B", n)
建议: 使用像'='这样的字节订单,同时处理像'我'这样的整数。