Jaman42
Jaman42

Reputation: 98

os.path.getsize on android returns incorrect value

I don't understand why, maybe I am missing something?

When I do a os.path.getsize on a file on my Android device I get this return:

394 005 664

But the file is actually 4 688 972 960 bytes, why?

Edit: Something with my Android system not supporting large files? Can this be altered? If I run ls -lh on the file I get 375.8M. So it's nothing worng with Python per say. As I commented on an answer below it works with small files.

Edit: If I run ls -lsh I get 4579076, is that occupied space in kb?

Edit: stat -c %s gives me 4688972960, and that is correct. But in Python os.stat('path').st_size gives me 394005664

Edit: I am going all in testing now :)

f = open("filepath")
old_file_position = f.tell()
f.seek(0, os.SEEK_END)
size = f.tell()
f.seek(old_file_position, os.SEEK_SET)

also gives me 394005664

Upvotes: 0

Views: 341

Answers (2)

Terry Bondy
Terry Bondy

Reputation: 1

Looks like os.path.getsize is only returning unsigned answers 32 bits wide, so will not return an answer greater than 4 GB, where GB = 1024 * 1024 * 1024 (not the disk drive manufacturer's definition), because:

4 688 972 960 % (2 ^ 32) = 394 005 664

Upvotes: 0

user2696806
user2696806

Reputation: 166

This usually only happens when working with directories. When the file your path specifies is a single file then it should work correctly but if the file is a folder with subfolders it will not behave as expected anymore. Python sees the size of a directory as the size of the directorydescription on disk and not the directorycontents.

There are other threads with solutions to this problem: Calculating a directory size using Python?

Upvotes: 1

Related Questions