Home > Backend Development > Python Tutorial > Python简单计算文件夹大小的方法

Python简单计算文件夹大小的方法

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-10 15:09:25
Original
1363 people have browsed it

本文实例讲述了Python简单计算文件夹大小的方法。分享给大家供大家参考。具体如下:

import os, re 
"""
查看文件夹下的所有文件及文件夹 join为拼接函数
"""
def Look_File(path):
  for root , dirs, files in os.walk(path, True):
    print root     #主目录
    for item in files: #主目录下的文件夹
      print os.path.join(root, item)
"""
计算文件夹 大小
"""    
def FileSize(path):
  size = 0L
  for root , dirs, files in os.walk(path, True):
    size += sum([os.path.getsize(os.path.join(root, name)) for name in files])
    #目录下文件大小累加
    return size
if __name__ == '__main__':
  Look_File("f:\\a")
  print FileSize("f:\\a")

Copy after login

希望本文所述对大家的Python程序设计有所帮助。

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template