gpt4 book ai didi

python - python中的稀疏文件使用

转载 作者:太空狗 更新时间:2023-10-29 20:17:37 25 4
gpt4 key购买 nike

我在 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 字节 block 的数量。请注意,st_blocks 不能保证在所有操作系统中都存在,但支持稀疏文件的操作系统通常会存在。

关于python - python中的稀疏文件使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3211999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com