我具有以下文件结构,并希望使用python创建每个文件夹中文件数量的字典。底部的示例将翻译为以下字典:
{Employee A: {Jan : 3}, {Feb : 2}, Employee B: {Jan : 2}, {Feb : 1}}
有人知道如何使用os来遍历目录吗?
Employee A
Jan
File 1
File 2
File 3
Feb
File 1
File 2
Employee B
Jan
File 1
File 2
Feb
File 1
答案 0 :(得分:2)
研究解析os.walk的输出
例如:
mydict = {}
for (root,dirs,files) in os.walk('testdir', topdown=False)
if len(files)>0:
mydict[root]=len(files)
print mydict
返回
{'testdir/EmployeeB/Jan': 2, 'testdir/EmployeeA/Feb': 2, 'testdir/EmployeeB/Feb': 1, 'testdir/EmployeeA/Jan': 3}
您可以轻松地解析这些键以生成您要查找的嵌套字典。
答案 1 :(得分:0)
通过这种方式,您可以遍历目录中的所有文件并创建它们的列表。您可以根据需要对其进行修改:
import os
import glob
from pathlib import Path
error_log_list = []
def traverse_structure():
try:
root = r"C:\\Users\Whatever\Desktop\DirectoryToSearch"
# Change working directory
os.chdir(root)
print("Creating master list of the directory structure...\n")
# Traverse the folder structure
for folder, subfolders, files in os.walk(root):
# Pass over each file
for file in files:
absolute_path = os.path.join(folder,file)
# Create a master file list
file_paths_list.append(absolute_path)
except Exception as e:
error_log_list.append( "Failed to open the root directory specified "+root+"\n Error: "+str(e)+"\n" )
traverse_structure()
答案 2 :(得分:0)
使用os库:
import os
parent = os.listdir(path) # return directory files to list
child = []
for x in parent:
if os.path.isdir(path +'/' + x):
child.append(os.listdir(path + '/' + x))
else
child.append('')
d = dict(zip(parent,child))
print(d)
这是使字典脱离目录的基本逻辑。但这支持2个级别。我将n级部分留给自己。
答案 3 :(得分:0)
只需进行一些细微调整,就可以根据需要执行ActiveState Python配方Create a nested dictionary from os.walk:
try:
reduce
except NameError: # Python 3
from functools import reduce
import os
def count_files_in_directories(rootdir):
""" Creates a nested dictionary that represents the folder structure
of rootdir with a count of files in the lower subdirectories.
"""
dir = {}
rootdir = rootdir.rstrip(os.sep)
start = rootdir.rfind(os.sep) + 1
for path, dirs, files in os.walk(rootdir):
folders = path[start:].split(os.sep)
subdir = len(files) if files else dict.fromkeys(files)
parent = reduce(dict.get, folders[:-1], dir)
parent[folders[-1]] = subdir
return list(dir.values())[0]
startdir = "./sample"
res = count_files_in_directories(startdir)
print(res) # -> {'Employee A': {'Feb': 2, 'Jan': 3}, 'Employee B': {'Feb': 1, 'Jan': 2}}
请注意./sample
目录是我创建的用于测试的文件夹结构的根目录,与您的问题中显示的目录结构完全相同。