用于获取文件夹结构以及大小

时间:2017-11-15 03:48:12

标签: python csv

您好我有两个代码块,如下所示 -

def GetHumanReadable(size,precision=2):
    suffixes=['B','KB','MB','GB','TB']
    suffixIndex = 0
    while size > 1024:
        suffixIndex += 1 #increment the index of the suffix
        size = size/1024.0 #apply the division
    return "%.*f %d"%(precision,size,suffixes[suffixIndex])

import os
def list_files(startpath):
    for root, dirs, files in os.walk(startpath):
        level = root.replace(startpath, '').count(os.sep)
        indent = ' ' * 4 * (level)
        print('{}{}/'.format(indent, os.path.basename(root)))
        subindent = ' ' * 4 * (level + 1)
        for f in sorted(files, key=lambda f: os.path.getsize(root + os.sep + f)):
            converted_size = GetHumanReadable(os.path.getsize(root + os.sep + f))
            print('{}{} - {}B'.format(subindent, f, converted_size))

执行以下操作时出现以下错误 -

list_files(r'C:\Users\Downloads')

错误 - TypeError:%d format:需要一个数字,而不是str

需要一个好的解决方案。我主要是寻找文件夹/子文件夹以及大小。上面的代码目前给出了每个文件的大小。要求修改为文件夹/子文件夹本身提供大小。最后需要在csv文件中写入输出。感谢

PS:我不拥有上述任何代码。所有代码都来自stackoverflow和其他类似平台,然后合并。

2 个答案:

答案 0 :(得分:1)

您需要做的就是将%d更改为%s并从list_files中的print语句末尾删除B:

In [227]: def GetHumanReadable(size,precision=2):
     ...:     suffixes=['B','KB','MB','GB','TB']
     ...:     suffixIndex = 0
     ...:     while size > 1024:
     ...:         suffixIndex += 1 #increment the index of the suffix
     ...:         size = size/1024.0 #apply the division
     ...:     return "%.*f %s"%(precision,size,suffixes[suffixIndex])
     ...: 
     ...: import os
     ...: def list_files(startpath):
     ...:     for root, dirs, files in os.walk(startpath):
     ...:         level = root.replace(startpath, '').count(os.sep)
     ...:         indent = ' ' * 4 * (level)
     ...:         print('{}{}/'.format(indent, os.path.basename(root)))
     ...:         subindent = ' ' * 4 * (level + 1)
     ...:         for f in sorted(files, key=lambda f: os.path.getsize(root + os.sep + f)):
     ...:             converted_size = GetHumanReadable(os.path.getsize(root + os.sep + f))
     ...:             print('{}{} - {}'.format(subindent, f, converted_size))
     ...: list_files(r'E:\Downloads\test')
test/
    file2.py - 40.00 B
    chap4exercise3.py - 57.00 B
    file1.py - 60.00 B
    run.py - 95.00 B
    largerfile.pdf - 767.08 KB
    __pycache__/
        file1.cpython-36.pyc - 239.00 B
        chap4exercise3.cpython-36.pyc - 253.00 B

答案 1 :(得分:0)

因为,你的后缀[suffixIndex]是一个包含'KB'字符串的字符串! 如此改变

return "%.*f %d"%(precision,size,suffixes[suffixIndex])

return "%.*f %s"%(precision,size,suffixes[suffixIndex])

将获得所需的输出!

将数据写入文件:

def list_files(startpath):
    with open('output.txt','w') as file:
        for root, dirs, files in os.walk(startpath):
            level = root.replace(startpath, '').count(os.sep)
            indent = ' ,' * 4 * (level)
            file.write('{}{}/\n'.format(indent, os.path.basename(root)))
            subindent = ' ,' * 4 * (level + 1)
            for f in sorted(files, key=lambda f: os.path.getsize(root + os.sep + f)):
                converted_size = GetHumanReadable(os.path.getsize(root + os.sep + f))
                file.write('{}{},{}\n'.format(subindent, f, converted_size))