【发布时间】:2011-03-13 19:46:43
【问题描述】:
我在 python 中创建稀疏文件如下:
>>> f = open('testfile', 'ab')
>>> f.truncate(1024000)
>>> f.close()
文件完成后,占用0磁盘空间,但它的inode大小设置为我的截断值(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
如何在 python 中获取文件的实际空间使用情况(分配大小)? stat 调用返回文件的表观大小,除了读取整个文件(它可能会变得非常大)之外,我不知道如何获得真正的用法
>>> os.stat('testfile').st_size
1024000
【问题讨论】:
-
附带说明,如果您使用 python 的 shutil 模块复制稀疏文件,这将不起作用,因为 shutil 不支持稀疏文件。
标签: python macos file filesize sparse-file