zoukankan      html  css  js  c++  java
  • python3获取文件及文件夹大小

    获取文件大小

    os.path.getsize(file_path):file_path为文件路径

    >>> import os
    >>> os.path.getsize('d:/svn/bin/SciLexer.dll')
    1479904

    获取文件夹大小

    遍历文件夹,将所有文件大小加和。遍历文件夹使用os.walk函数

    import os
    from os.path import join, getsize
    
    
    def getdirsize(dir):
        size = 0
        for root, dirs, files in os.walk(dir):
            size += sum([getsize(join(root, name)) for name in files])
        return size
    
    
    if __name__ == '__main__':
        size = getdirsize(r'D:svn')
        print('There are %.3f' % (size / 1024 / 1024), 'Mbytes in D:\svn')
    执行结果:
     

     help(os.walk)获取帮助信息

    Help on function walk in module os:
    
    walk(top, topdown=True, onerror=None, followlinks=False)
        Directory tree generator.
        
        For each directory in the directory tree rooted at top (including top
        itself, but excluding '.' and '..'), yields a 3-tuple
        
            dirpath, dirnames, filenames
        
        dirpath is a string, the path to the directory.  dirnames is a list of
        the names of the subdirectories in dirpath (excluding '.' and '..').
        filenames is a list of the names of the non-directory files in dirpath.
        Note that the names in the lists are just names, with no path components.
        To get a full path (which begins with top) to a file or directory in
        dirpath, do os.path.join(dirpath, name).
        
        If optional arg 'topdown' is true or not specified, the triple for a
        directory is generated before the triples for any of its subdirectories
        (directories are generated top down).  If topdown is false, the triple
        for a directory is generated after the triples for all of its
        subdirectories (directories are generated bottom up).
        
        When topdown is true, the caller can modify the dirnames list in-place
        (e.g., via del or slice assignment), and walk will only recurse into the
        subdirectories whose names remain in dirnames; this can be used to prune the
        search, or to impose a specific order of visiting.  Modifying dirnames when
        topdown is false is ineffective, since the directories in dirnames have
        already been generated by the time dirnames itself is generated. No matter
        the value of topdown, the list of subdirectories is retrieved before the
        tuples for the directory and its subdirectories are generated.
        
        By default errors from the os.scandir() call are ignored.  If
        optional arg 'onerror' is specified, it should be a function; it
        will be called with one argument, an OSError instance.  It can
        report the error to continue with the walk, or raise the exception
        to abort the walk.  Note that the filename is available as the
        filename attribute of the exception object.
        
        By default, os.walk does not follow symbolic links to subdirectories on
        systems that support them.  In order to get this functionality, set the
        optional argument 'followlinks' to true.
        
        Caution:  if you pass a relative pathname for top, don't change the
        current working directory between resumptions of walk.  walk never
        changes the current directory, and assumes that the client doesn't
        either.
        
        Example:
        
        import os
        from os.path import join, getsize
        for root, dirs, files in os.walk('python/Lib/email'):
            print(root, "consumes", end="")
            print(sum([getsize(join(root, name)) for name in files]), end="")
            print("bytes in", len(files), "non-directory files")
            if 'CVS' in dirs:
                dirs.remove('CVS')  # don't visit CVS directories

    ***********************************************************

     学习永远不晚。——高尔基

    ***********************************************************

  • 相关阅读:
    创建Android守护进程(底层服务)【转】
    jack server 常见错误解决方法【转】
    kvm初体验——linux之kvm安装及使用qemu工具安装系统【转】
    全志H3-NanoPi开发板SDK之三编译流程【转】
    数字图像处理之二维码图像提取算法(一)【转】
    Ubuntu登陆不进去(已解决)【转】
    可重入函数设计的基本原理【学习笔记】
    Linux文件系统十问---深入理解文件存储方式(rhel6.5,EXT4)【转】
    [RK3288][Android6.0] TS-ADC驱动流程小结【转】
    手把手教你使用eclipse+qemu+gdb来单步调试ARM内核【学习笔记】
  • 原文地址:https://www.cnblogs.com/chengd/p/7298874.html
Copyright © 2011-2022 走看看