1
file1 = open("test.txt", 'wb')
file1.write(struct.pack('icic', 1, '\t', 2, '\n'))
file1.close()
print os.path.getsize("test.txt")

It gives me 13. But I think it should be 4 + 1 + 4 + 1 = 10 bytes. It seems that it stores '\n' for one byte, but '\t' for 4 bytes. And idea? Thanks!

4
  • Why don't you just have a look at struct.pack('icic', 1, '\t', 2, '\n') itself? Commented Jul 21, 2014 at 8:22
  • You mean the source code? Commented Jul 22, 2014 at 15:59
  • No, I mean the result of the function call. On my machine, this gives '\x01\x00\x00\x00\t\x00\x00\x00\x02\x00\x00\x00\n' which means 4 bytes (little endian) for the 1, 4 bytes for the \t, 4 bytes (LE) for the 2 and one byte for the \n. And this, in turn means, three 0-bytes are inserted after the \t. If you have a look at this, you get some steps further. Commented Jul 22, 2014 at 16:11
  • BTW, you should have your file formats portable, so that you can easily share them between machines of different architecture. For this, you should use the > prefix. In this case, you have the byte order you wand, and you get no fill bytes. Commented Jul 22, 2014 at 16:12

1 Answer 1

4

To get the actual struct size, use struct.calcsize():

>>> import struct
>>> struct.calcsize('icic')
13

That's because you are using the default alignment, and then C rules are applied:

By default, C types are represented in the machine’s native format and byte order, and properly aligned by skipping pad bytes if necessary (according to the rules used by the C compiler).

The first ic would be just 5 bytes, but C would pad that out to 8 if you list it twice, so the next ic pair brings it to 13. If you used 3 ic pairs you'd get to 21, etc. C pads out the i integers to align to 4-byte groups. This data structure alignment is used to improve memory performance, but can be unexpected when trying to use it for different purposes.

Pick an explicit byte order instead:

>>> struct.calcsize('>icic')
10

See the Byte Order, Size and Alignment section.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. I never thought it is because of padding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.