在Python中稀疏文件的使用 [英] sparse file usage in python

查看:165
本文介绍了在Python中稀疏文件的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python中创建稀疏文件如下:

 >>> f = open('testfile','ab')
>>> f.truncate(1024000)
>>> f.close()

当文件完成时,占用0个磁盘空间,但其inode大小设置为我截断的值(1000K):

$ $ p $ 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

如何在python中获取文件的实际空间使用情况? stat 调用返回文件的外观大小,我不知道除了读整个文件外, p>

 >>> os.stat('testfile')。st_size 
1024000


解决方案

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

Tadaa:)



st_blocks 是实际分配给文件的512字节块的数量。请注意, st_blocks 不能保证在所有的操作系​​统中都存在,但是那些支持稀疏文件的文件通常是这样。


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

解决方案

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

这篇关于在Python中稀疏文件的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆