I'm creating sparse files in python as follows:
>>> f = open('testfile', 'ab')
>>> f.truncate(1024000)
>>> f.close()
when the file is done, it takes up 0 disk space, but its inode size is set to my truncated value (1000K):
igor47@piglet:~/test$ ls -lh testfile
-rw-r--r-- 1 igor47 igor47 1000K 2010-07-09 04:02 testfile
igor47@piglet:~/test$ du -hs testfile
0 testfile
how do i get the file's real space usage inside python? the stat call returns the file's apparent size, and i have no idea how else to get the real usage other than to read the entire file (it may become quite large)
>>> os.stat('testfile').st_size
1024000
3188







st_blksizeprovides actual block size of the filesystem. Generally speaking it doesn't have to be 512 bytes. – SilentGhost Jul 9 '10 at 11:33st_blksizeis the "preferred" file system block size.st_blocksis guaranteed to be in blocks of 512 bytes (see the manpage). – wump Jul 9 '10 at 12:49