Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does os.path.getsize() return a negative number for a 10gb file?

I am using the function os.path.getsize() which gives the size of the file in bytes.

As my one file size is 10gb it give me size in negative(bytes).

so can anyone give me any idea why this happen?

This is my code:

import os
ospathsize = os.path.getsize('/home/user/Desktop/test1.nrg')
print (ospathsize) 
like image 559
Dipen Avatar asked Mar 02 '11 11:03

Dipen


People also ask

What does OS path Getsize return?

path. getsize() method in Python is used to check the size of specified path. It returns the size of specified path in bytes.

What does OS path Getsize Path_str return?

What does os. path. getsize(path_str) return? The size in bytes of the file at path_str.

Is Path exist Python?

exists() method in Python is used to check whether the specified path exists or not. This method can be also used to check whether the given path refers to an open file descriptor or not.


2 Answers

Your Linux kernel obviously has large file support, since ls -l works correctly. Thus, it's your Python installation that is lacking the support. (Are you using your distribution's Python package? What distribution is it?)

The documentation on POSIX large file support in Python states that Python should typically make use of large file support if it is available on Linux. It also suggests to try and configure Python with the command line

CFLAGS='-D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64' OPT="-g -O2 $CFLAGS" \
    ./configure

And finally, quoting the man page of the stat system call:

This can occur when an application compiled on a 32-bit platform without -D_FILE_OFFSET_BITS=64 calls stat() on a file whose size exceeds (1<<31)-1 bits.

(I believe the last word should be "bytes".)

like image 151
Sven Marnach Avatar answered Nov 09 '22 07:11

Sven Marnach


Looks like an overflow of 32-bit int used for size which is limited to 4GB. This may be a bug (or even a missing compilation flag) in your particular version of Python. I just tried it in a 32-bit Linux box, using python 2.4 and 2.6; both give correct results on files bigger than 4GB.

Try upgrading your Python; the fix is probably a minor version away.

like image 40
9000 Avatar answered Nov 09 '22 08:11

9000