python获取文件及文件夹大小

Python3.3下测试通过

获取文件大小

使用os.path.getsize函数,参数是文件的路径

获取文件夹大小

  1. import os  
  2. from os.path import join, getsize  
  3.   
  4. def getdirsize(dir):  
  5.    size = 0.0  
  6.    for root, dirs, files in os.walk(dir):  
  7.       size += sum([getsize(join(root, name)) for name in files])  
  8.    return size  
  9.   
  10. if __name__ == “__main__”:  
  11.    filesize = getdirsize(r'c:windows')  
  12.    print (("There are %.3f") % (filesize/1024/1024), ("Mbytes in c:\windows"))

注print 默认自动加上回车,如果不想加上回车 则在后面加‘,’

原文地址:https://www.cnblogs.com/seer/p/3300564.html