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

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

问题描述


print(目录大小)
print(os.path.getsize(dirname))

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

解决方案

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



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

  len(os.path.listdir(dirname))

要获得总数据量,您可以在此问题中使用代码 ,即(如@linker发布)

  sum([os.path.getsize(f)for f在os.listdir('。')如果os.path.isfile(f)])。 


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

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?

解决方案

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.path.listdir(dirname))

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天全站免登陆