Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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 (allocated size) 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
                on a side note, if you are copying sparse files using python's shutil module, this will not work, as shutil doesn't support sparse files.
– Igor Serebryany
                Jul 13, 2010 at 20:35
                st_blksize provides actual block size of the filesystem. Generally speaking it doesn't have to be 512 bytes.
– SilentGhost
                Jul 9, 2010 at 11:33
                I was confused for a moment, but that's not true. st_blksize is the "preferred" file system block size. st_blocks is guaranteed to be in blocks of 512 bytes (see the manpage).
– wump
                Jul 9, 2010 at 12:49
                This particular solution was working this morning, but after i've changed the server machine it suddenly stopped working. Do you have any advice on that?
– Hgeg
                Oct 8, 2012 at 0:42
                @Wang no: du -b shows the sparse file size, like ls -l do. du -s and ls -ls shows the real allocated space ( ls -ls shows this in the first column)
– benba
                May 18, 2022 at 8:36
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.