我现在需要获取一个exe文件的大小,但是由于exe被额外捆绑上了一个文件,所以无法通过os.path.getsize来解决,只能通过读取exe的2-3h和4-5h来计算文件大小
with open("ngrok.exe","rb") as f:
f.seek(2,0)
remainder = int(f.read(2))
quotient = int(f.read(2))
print(quotient*512+remainder)
with open("ngrok.exe","rb") as f:
f.seek(2,0)
remainder = int(f.read(2))
quotient = int(f.read(2))
print(quotient*512+remainder)
int(f.read(2))
可以改成 int.from_bytes(f.read(2), "little")
, 用int.from_bytes
方法。(补充: 与之对应, int
数字有to_bytes
方法。)
其中的little
是字节的顺序参数, 可以是little
或big
。little
是低位在前, 而big
是高位在前。参考: 大端序和小端序 - 知乎