python 计算文件夹下文件的大小
2017-04-23 本文已影响376人
Echo112233
#!/usr/bin/env python3
#coding: utf-8
import os
def curdir_size():
''' 计算当前文件夹下的所有文件的大小 '''
all_files = os.listdir(os.curdir)
file_dict = dict()
print(all_files)
for each_file in all_files:
if os.path.isfile(each_file):
file_size = os.path.getsize(each_file)
file_dict[each_file] = file_size
for each in file_dict.items():
#print('%s【%dBytes】' % (each[0],each[1]))
print('%s【%dMbytes】' % (each[0],each[1]/1024/1024))
def any_directory_size():
''' 计算指定的路径下的所有文件的大小 '''
directory = input('input a directory:')
if not os.path.isdir(directory):
print(directory, ' 不是目录')
exit(0)
all_files = os.listdir(directory)
file_dict = dict()
for each_file in all_files:
file_path = os.path.join(directory,each_file)
if os.path.isfile(file_path):
file_size = os.path.getsize(file_path)
file_dict[each_file] = file_size
for each in file_dict.items():
#print('%s【%dBytes】' % (each[0],each[1]))
print('%s【%dMbytes】' % (each[0],each[1]/1024/1024))
# 计算当前文件夹下的所有文件的大小
# curdir_size()
# 计算指定的路径下的所有文件的大小
any_directory_size()