Mahendra Liya
Mahendra Liya

Reputation: 13218

os.path.getsize() returns negative filesize for large files (for > 3GB file size)

I am using python on embedded device which is running linux 2.6.32.

Using python to fetch the file size as os.path.getsize() returns me a negative value.

I refered to one of the similar questions here on stack overflow and tried recompiling python with CFLAGS='-D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64' OPT="-g -O2 $CFLAGS" but still it returns negative file size.

I am using python 2.6.4 with Django 1.2.4 on linux 2.6.32.

Can any one tell me where the problem is?

Upvotes: 3

Views: 2608

Answers (1)

Rakis
Rakis

Reputation: 7864

Clearly, something is wrong with your Linux distribution's build of Python. Rather than fix the actual problem, it might be easier to just work around it:

def getsize_workaround( filename ):
    size = os.path.getsize( filename )
    if size < 0:
        import subprocess as s
        size = long( s.Popen("ls -l %s | cut -d ' ' -f5" % filename,
                    shell=True, stdout=s.PIPE).communicate()[0] )
    return size

Upvotes: 2

Related Questions