Skip to content

Instantly share code, notes, and snippets.

@gsw945
Created May 24, 2018 10:29
Show Gist options
  • Save gsw945/cf1a0986dbd05b540c9b1654c1061fbb to your computer and use it in GitHub Desktop.
Save gsw945/cf1a0986dbd05b540c9b1654c1061fbb to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import os
def print_size(size):
'''打印文件大小'''
print('Size: {0} Byte'.format(size))
print('Size: {0} KB'.format(size / 1024))
print('Size: {0} MB'.format(size / 1024 / 1024))
print('Size: {0} GB'.format(size / 1024 / 1024 / 1024))
def calc_file_size(root):
'''计算目录下所有文件大小之和'''
total_size = 0
for path, subdirs, files in os.walk(root):
for _file in files:
fn = os.path.join(path, _file)
file_size = 0
try:
file_size = os.path.getsize(fn)
except:
pass
total_size += file_size
print_size(total_size)
return total_size
root = r'C:\Users\Public'
calc_file_size(root)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment