python 统计当前文件夹的大小
2019-12-01 本文已影响0人
洛丽塔的云裳
使用 os.path.getsize()
返回文件大小,以字节为单位
# -*- coding: utf-8 -*-
import os
import sys
def get_subfile_size(path):
""" show all subfile size """
file_info = []
all_file = os.listdir(path)
for file in all_file:
f_info = {}
f_name = os.path.join(path, file)
f_size = os.path.getsize(f_name)
f_info[f_name] = f_size
file_info.append(f_info)
return file_info
if __name__ == '__main__':
path = os.getcwd() + "/learnPython"
file_info = get_subfile_size(path)
print file_info
测试结果
{
'/home/work/test/code-test/learnPython/create_dir_if_not_there2.py': 501
}, {
'/home/work/test/code-test/learnPython/fast_youtube_downloader.py': 699
}, {
'/home/work/test/code-test/learnPython/.idea': 4096
}, {
'/home/work/test/code-test/learnPython/batch_file_rename2.py': 1194
}, {
'/home/work/test/code-test/learnPython/dir_test.py': 332
}, {
'/home/work/test/code-test/learnPython/create_dir_if_not_there.py': 836
}, {
'/home/work/test/code-test/learnPython/batch_file_rename.py': 1101
}, {
'/home/work/test/code-test/learnPython/env_check.py': 737
}, {
'/home/work/test/code-test/learnPython/google_image_downloader.py': 747
}