MacSanhe
MacSanhe

Reputation: 2240

Python os.path.getsize(path) get the wrong size of a *.jpg file

I am trying to sort and rename all the *.jpg file under current working directory. But when I use os.path.getsize(path), it returns the wrong size. For example, I have 2 pictures: 13.jpg and 15.jpg.

you can see in the explorer:

13.jpg  474kb
15.jpg  464kb

13>15

but in python:

current working directory = "something we don't care"
name = 13.jpg
info = os.stat(name)
print(name, info.st_size, os.path.getsize(name))

and in the console I can see

13.jpg 472355 472355
14.jpg 474241 474241
15.jpg 474391 474391

Now is 15>13

How can this be?

Upvotes: 1

Views: 1243

Answers (1)

MxLDevs
MxLDevs

Reputation: 19506

Explorer can and has gotten things wrong for me plenty of times.

Verify the actual byte-count by right-clicking the files and looking at properties. Also, rather than comparing KB with Bytes, you should compare Bytes with Bytes. Explorer might be rounding it wrong or something odd.

As for whether python's os.path.getsize method is always correct, I don't know about that, but presumably it relies on your file-system getting it right.

Upvotes: 1

Related Questions