日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python统计文件大小_python工具--01.统计当前目录下的文件的大小

發(fā)布時(shí)間:2023/12/2 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python统计文件大小_python工具--01.统计当前目录下的文件的大小 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

環(huán)境

os: centos7

python : 3.7

實(shí)現(xiàn)功能

統(tǒng)計(jì)當(dāng)目錄下的文件夾有文件的大小,單位KB/MB/B;

代碼實(shí)現(xiàn)

#!/usr/bin/env python

# _*_ coding:utf-8 _*_

import os,math

summary=0

def size_file(str2):

global summary

summary=summary+os.path.getsize(str2)

def size_dir(str1):

dlist=os.listdir(str1)

# print("正在統(tǒng)計(jì)目錄:"+str1)

for f in dlist:

file1=os.path.join(str1,f)

if os.path.isfile(file1):

size_file(file1)

else:

size_dir(file1)

def init_os(path_init):

if os.path.exists(path_init):

# 文件存在計(jì)算文件的大小;

size_dir(path_init)

if 1024 <= math.ceil(summary) <= 1000000:

print("[{}]目錄: ".format(path_init)+str(summary/1024)+'KB')

elif math.ceil(summary) > 1000000:

print("[{}]目錄: ".format(path_init)+str((summary/1024)/1024)+'MB')

else:

print("[{}]目錄: ".format(path_init)+str(summary)+'B')

else:

print("目錄不存在!,請(qǐng)重新輸入.")

def main(local_path):

dblist=os.listdir(local_path)

for d in dblist:

file2 = os.path.join(local_path, d)

# 統(tǒng)計(jì)當(dāng)前目錄下文件的大小;

if os.path.isfile(file2):

size_file(file2)

if 1024 <= math.ceil(summary) <= 1000000:

print("[{}]: ".format(file2) + str(summary / 1024) + 'KB')

elif math.ceil(summary) > 1000000:

print("[{}]: ".format(file2) + str((summary / 1024) / 1024) + 'MB')

else:

print("[{}]: ".format(file2) + str(summary) + 'B')

# 統(tǒng)計(jì)當(dāng)前目錄下文件夾大小

else:

init_os(file2)

if __name__ == '__main__':

path=os.getcwd()

main(path)

總結(jié)

以上是生活随笔為你收集整理的python统计文件大小_python工具--01.统计当前目录下的文件的大小的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。