Python - 列出指定文件夹中的子文件夹大小

时间:2016-06-02 17:32:32

标签: python operating-system

我需要在包含许多文件的驱动器上腾出空间。我想找到哪个文件夹占用了名为C:\ RS_Data的特定位置中空间最多的文件夹。一旦我确定哪个sufolders占用了大部分空间,我想深入到这些子文件夹并执行相同的任务。所以我想开发一个脚本,它将与我一起指定返回文件和文件夹大小的路径,  它可能是这样的:  C:\ RS_Data:     Sub_Folder_1:566,000 kb     Sub_Folder_2:323,333 kb     Sub_folder_3:3,123,456 kb     random_file.ext:3,123 kb

依旧......

然后我想在Sub_folder_3上运行相同的脚本。

我在论坛上找到了以下脚本并添加了paths.append(第22行的路径:

import locale
import os

locale.setlocale(locale.LC_ALL, "")

def get_size(state, root, names):
    paths = [os.path.realpath(os.path.join(root, n)) for n in names]
    # handles dangling symlinks
    state[0] += sum(os.stat(p).st_size for p in paths if os.path.exists(p))

def print_sizes(root):
    total = 0
    paths = []
    state = [0]
    n_ind = s_ind = 0
    for name in sorted(os.listdir(root)):
        path = os.path.join(root, name)
        if not os.path.isdir(path):
            continue
        paths.append(path)

        state[0] = 0
        os.path.walk(path, get_size, state)
        total += state[0]
        s_size = locale.format('%8.0f', state[0], 3)
        n_ind = max(n_ind, len(name), 5)
        s_ind = max(s_ind, len(s_size))
        paths.append((name, s_size))

    for name, size in paths:
        print name.ljust(n_ind), size.rjust(s_ind), 'bytes'
    s_total = locale.format('%8.0f', total, 3)
    print '\ntotal'.ljust(n_ind), s_total.rjust(s_ind), 'bytes'

print_sizes('.')

我收到一个错误,即解压缩的值太多了。有没有办法让我可以在我指定的路径上运行脚本?

感谢。

我采用了下面推荐的示例并对其进行了修改,因此该函数可以在for循环中运行。这将为父文件夹中的所有文件和文件夹返回0。这是修改后的代码:

import os

myDir = "C:\\RS_Data"
folders = os.listdir(myDir)

for file in folders: 
    def get_size(start_path = file):
        global total_size
        total_size = 0
        for dirpath, dirnames, filenames in os.walk(start_path):
            for f in filenames:
                fp = os.path.join(dirpath, f)
                total_size += os.path.getsize(fp)
        return total_size

    print os.path.basename(file),":", get_size()

结果如下:

Attribute.gdb : 0
Bookmarks : 0
calculations : 0
Common : 0
Copy of CO_054_Run Batch Files.bat : 0
CO_003_Run Batch Files.bat : 0
CO_052_Run Batch Files.bat : 0
CO_053_Run Batch Files.bat : 0
CO_054_2_Run_Batch_Files.bat : 0
CO_054_Run Batch Files.bat : 0
CO_073_Run Batch Files_Old.bat : 0
CO_073_Run_Batch_Files.bat : 0
CO_073_Run_Batch_FilesREV1.bat : 0
gdb backups : 0
GeocodingInformation.gdb : 0
GeocodingInformation.ldb : 0
GeocodingInformation.mdb : 0
GeocodingInformationold.mdb : 0
GIS_Projects : 0
Models : 0
Network : 0
Non_RS_MXDs : 0
old2Run Batch Files.bat : 0
old3Run Batch Files.bat : 0
OldCO_052_053_054_Run Batch Files2.bat : 0
OldCO_052_053_054_Run Batch FilesIJ.bat : 0
oldCO_052_Run Batch Files.bat : 0
oldCO_053_Run Batch Files.bat : 0
oldCO_073_Run Batch Files.bat : 0
orig_rs_system.mdb : 0
orig_rs_system_backup.mdb : 0
PatternGroup.gdb : 0
Python : 0
Recovered : 0
robocopy.exe : 0
ROBOUSERS.OUT : 0
rs_system.mdb : 0
rs_system_backup.mdb : 0
rs_system_backupnew.ldb : 0
rs_system_backupnew.mdb : 0
Run Batch Files_old.bat : 0
Scrap : 0
Temp : 0
Templates.gdb : 0
Workspace : 0
WorkspacesSettings.gdb : 0

如果我在没有循环的情况下运行脚本,则返回指定文件夹的累积大小。

1 个答案:

答案 0 :(得分:0)

有一个类似的问题:Calculating a directory size using Python?

import os
    def get_size(start_path = '.'):
    total_size = 0
    for dirpath, dirnames, filenames in os.walk(start_path):
        for f in filenames:
            fp = os.path.join(dirpath, f)
            total_size += os.path.getsize(fp)
    return total_size
print get_size()