Welcome, guest | Sign In | My Account | Store | Cart

You can't use the os library to determine the size of large files on 32 bit Windows. It may not be obvious from the name, however, a win32 call to FindFiles, can provide a solution.

Python, 25 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import win32api

#make long integers are used
neg_gig2=-2147483648L
gig2=2147483648L
gig4=4294967296L

#additional stores every whole group of 4 gigs
#base stores the remainder as an (unfortunately)signed int
#ignore the rest of what is returned in the array
file='c:/pagefile.sys'
(additional,base)=win32api.FindFiles(file)[0][4:6]

size=base

#if you get a negative size, that means the size is between 2 and 4 gigs
#The more negative the smaller the file
if base<0L:
 size=long(abs(neg_gig2-base))+gig2

#For every additional you add 4 gigs 
if additional:
 size=size+(additional*gig4)

print size,'bytes or ',size/1024,'kb'
 With 32 bit systems an integer 0xFFFFFFFF can have a value up to 4294967296.

If you use a signed integer in python, it is divided between negative and positive ints. You have values in the range -2147483647 to 2147483647. This presents a problem if you wish to do things like determine disk usage for very large files on windows. On windows there are 2 ways to solve this, thanks to Mark Hammond's modules. One is to call CreateFile and get a handle to the file. Unfortunately, the file may be locked and thus, you cannot get the information. Another solution is to use the FindFiles win32call which provides among other things file size information.