Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sparse file usage in python

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 (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
like image 803
Igor Serebryany Avatar asked Jul 09 '10 11:07

Igor Serebryany


People also ask

What is the use of sparse file?

In computer science, a sparse file is a type of computer file that attempts to use file system space more efficiently when the file itself is partially empty.

What is the use of sparse file in big data?

Sparse Files are a type of computer file that allows for efficient storage allocation for large data. A file is considered to be sparse when much of its data is zero (empty data). Support for the creation of such files is generally provided by the File system.

What is a sparse file record?

A file format that saves storage space by recording only actual data. Whereas regular files record empty fields as blank data or runs of nulls, a sparse file includes meta-data that describe where the runs of non-data are located.


1 Answers

>>> os.stat('testfile').st_blocks*512
0

Tadaa :)

st_blocks is the number of 512-byte blocks actually allocated to the file. Note that st_blocks is not guaranteed to be present in all operating systems, but those that support sparse files generally do.

like image 149
wump Avatar answered Sep 20 '22 13:09

wump