sparse file usage in python

戏子无情 提交于 2019-12-23 07:33:10

问题


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

回答1:


>>> 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.



来源:https://stackoverflow.com/questions/3211999/sparse-file-usage-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!