This question might look silly but, I have a /tmp/size.txt with this content:
hello
and os.path.getsize('/tmp/size.txt') says 6 but when I do:
sys.getsizeof(b'hello')
# OR
sys.getsizeof(bytes(bytearray('hello')))
# OR
sys.getsizeof(bytes('hello'))
it returns 42.
What is the difference between the os.path.getsize and sys.getsizeof?
The two are not compatible in python. os.path.getsize give the size of a file, whereas sys.getsizeof gives the size of an object.
The file is 6 bytes, not 5, because of a line-ending (on Windows it might be 7 bytes). If you were using C then "hello" would be 6 bytes because a binary zero '\0' marks the end of the string. If you were using another language then it too would have its own red-tape memory overhead.
The memory occupied by the data is (generally) less than that occupied by an object. An object will include other information about the data, like its size and location. It is a price you pay for using a high-level language.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With