Fetch the repository succeeded.
print("====统计指定目录大小====")
import os
#计算指定目录大小函数
def dir_size(dir):
sum=0
if os.path.isfile(dir):
sum+=os.path.getsize(dir)
if os.path.isdir(dir):
ldir=os.listdir(dir)
for i in ldir:
file=os.path.join(dir,i)
if os.path.isdir(file):
sum+=dirsize(file)
if os.path.isfile(file):
sum+=os.path.getsize(file)
return sum
#输入需统计目录or文件
while True:
key_word = input("请输入指定文件目录的文件夹或文件(D:\Python或D:\Python\1.py),直接回车即退出:\n")
if key_word == "":
break
else:
print("{}字节".format(dir_size(key_word)))
Sign in for post a comment
Comment ( 0 )