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

有帮助吗?

解决方案

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

坦达:)

st_blocks是实际分配给文件的512字节块的数量。请注意,所有操作系统中不保证生成的世代odicetagcode,但支持稀疏文件的那些通常会发生。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top