Afshin Mehrabani
Afshin Mehrabani

Reputation: 34929

Why sys.getsizeof is not equal to os.path.getsize?

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?

Upvotes: 3

Views: 1922

Answers (2)

cdarke
cdarke

Reputation: 44354

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.

Upvotes: 7

Dean Fenster
Dean Fenster

Reputation: 2395

os.path.getsize returns the file size in bytes.

sys.getsizeof returns the amount of bytes needed to store the str/bytes object in memory. (which has an overhead over the actual content, due to structure data).

Upvotes: 0

Related Questions