[英]Find summed size of files with specific extensions within a directory?
I want to sum the sizes of files that match a particular extension (and do so for several extensions).我想对与特定扩展名匹配的文件大小求和(并为多个扩展名这样做)。 Below is partially working code to do so, but I need help applying this to all extensions found in a Directory.
下面是部分工作代码,但我需要帮助将其应用于目录中的所有扩展。
import glob
import os
path = '/tmp'
files = glob.glob(path + "/**/*.txt")
total_size = 0
for file in files:
total_size += os.path.getsize(os.path.join(path, file))
print len(files), total_size
So, I want to end up with variables containing the values of how much total .txt or .mp3 file data there is.所以,我想最终得到包含 .txt 或 .mp3 文件数据总量值的变量。 Something like:
就像是:
Data1[] = { .mp3, 1209879834 bytes);
Data2[] = { .txt, 134213443 bytes);
DataX[] = { .X, X bytes);
I have taken the liberty of assuming your intention was to find the total sum of the sizes of all files matching a certain set of extensions within a directory (and my pending edit to your question will reflect that if approved):我冒昧地假设您的意图是找到与目录中特定扩展名集匹配的所有文件的大小总和(如果批准,我对您的问题的待定编辑将反映这一点):
import glob
import os
def summed_sizes(extensions: list, directory: str='.'):
total = 0
grouped_files = [glob.glob(os.path.join(directory, f"**/*.{ext}")) for ext in extensions]
for ext_group in grouped_files:
for file in ext_group:
total += os.path.getsize(file)
return total
print(summed_sizes(['jpg', 'txt'], '/tmp'))
You can search for all names in the subdirectory and filter the extensions yourself.您可以搜索子目录中的所有名称并自行过滤扩展名。
glob
is doing something similar by comparing all names with fnmatch
. glob
通过将所有名称与fnmatch
进行比较来做类似的事情。 Notice that glob
returns the full path, so you don't need to add it again.请注意
glob
返回完整路径,因此您不需要再次添加它。 You can use list comprehensions to build the lists.您可以使用列表理解来构建列表。
import glob
import os
path = '/tmp'
extensions = set(('.txt', '.foo', '.bar'))
files = [fn for fn in glob.glob(path + "/**/*")
if os.path.splitext(fn)[1] in extensions]
total_size = sum(os.path.getsize(fn) for fn in files)
print len(files), total_size
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.