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

I use the os.path.getsize() function, which gives the file size in bytes.

Like my single file size is 10gb, it gives me a negative size (bytes).

can anyone let me know why this is happening?

This is my code:

 import os ospathsize = os.path.getsize('/home/user/Desktop/test1.nrg') print (ospathsize) 
+7
source share
2 answers

The Linux kernel obviously has a lot of file support since ls -l working correctly. So this is your Python installation that lacks support. (Do you use your Python distribution? What is this distribution?)

The documentation for POSIX large file support in Python states that Python should usually use more file support if it is available on Linux. He also suggests trying and configuring Python with the command line.

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

And finally, on the stat system call page:

This can happen if an application compiled on a 32-bit platform without -D_FILE_OFFSET_BITS=64 calls stat() in a file that is larger than a bit (1<<31)-1 .

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

+10
source

Looks like a 32-bit int overflow used for a size limited to 4 GB. It could be a bug (or even a missing compilation flag) in your particular version of Python. I just tried this in a 32 bit Linux box using python 2.4 and 2.6; both give correct results in files larger than 4 GB.

Try updating your Python; the fix is ​​probably a minor version.

+1
source

All Articles