Python Little Practice---Import OS module to do a statistical folder size function

Source: Internet
Author: User

Import OS

def getdirsize (Dirpath):

#设置一个用于累加大小的变量
Total = 0

#获取文件夹中所有文件和文件夹
Allnames = Os.listdir (Dirpath)
#遍历所有文件和文件夹, and add up their size
For I in Allnames:
#遍历同时组合完整路径
FullPath = Os.path.join (dirpath,i)


#判断是文件还是文件夹
If Os.path.isfile (FullPath):
#print (FullPath, '---file ')
#获取文件大小
Total + = Os.path.getsize (FullPath)


Elif Os.path.isdir (FullPath):
#print (FullPath, '---directory ')
#获取文件夹大小
Total + = Getdirsize (FullPath)


Else
#print (fullpath, '---link ')
# Get link size
Total + = Os.path.getsize (FullPath)

#返回总大小
Return Total

#调用函数
result = Getdirsize ('/etc/acpi ') #要统计的文件夹
Print (Result)

Program code Picture

Python Little Practice---Import OS module to do a statistical folder size function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.