os.path.getsize 返回不正确的值? [英] os.path.getsize Returns Incorrect Value?

查看:44
本文介绍了os.path.getsize 返回不正确的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

def size_of_dir(dirname):
    print("Size of directory: ")
    print(os.path.getsize(dirname))

是有问题的代码.dirname 是一个包含 130 个文件的目录,每个文件大约 1kb.当我调用这个函数时,它返回4624,这是不是目录的大小......为什么会这样?

is the code in question. dirname is a directory with 130 files of about 1kb each. When I call this function, it returns 4624, which is NOT the size of the directory...why is this?

推荐答案

此值 (4624B) 表示描述该目录的文件的大小.目录被描述为 inode (http://en.wikipedia.org/wiki/Inode)保存有关其包含的文件和目录的信息.

This value (4624B) represents the size of the file that describes that directory. Directories are described as inodes (http://en.wikipedia.org/wiki/Inode) that hold information about the files and directories it contains.

要获取该路径内的文件/子目录的数量,请使用:

To get the number of files/subdirectories inside that path, use:

len(os.listdir(dirname))

要获取总数据量,您可以在此问题中使用代码/a>,即(如@linker 发布的那样)

To get the total amount of data, you could use the code in this question, that is (as @linker posted)

 sum([os.path.getsize(f) for f in os.listdir('.') if os.path.isfile(f)]).

这篇关于os.path.getsize 返回不正确的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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