【发布时间】:2020-07-14 21:45:02
【问题描述】:
所以我正在寻求一些帮助来编写如下图所示的函数。我将发布所提供函数的代码,并发布我目前所做的工作。
提供的功能:
def print_file_listing(file_listing, indent=""):
"""Format and print file_listing, a dictionary representing the
description of a disk file.
Parameters:
file_listing: a dictionary with keys "name", "timestamp",
"type" and "size". The "type" value (always "file") is
omitted from the printed output.
indent (optional): a string of spaces representing the
level at which the file is nested in the folder
structure.
"""
print(f"{indent}{file_listing['name']:<12} "\
f"{file_listing['timestamp']} "\
f"{file_listing['size']:>8}")
到目前为止我的代码:
def print_folder(folder, indent="0"):
"""Format and print folder, a dictionary representing a file
system.
Parameters:
folder: a dictionary with keys "name", "timestamp", "type" and
"files". "files" is a list of zero or more files and
folders. The "type" value (always "dir") is omitted from
the printed output.
indent (optional): a string of spaces representing the
level at which the folder is nested in the file system.
"""
print(f"{indent}{folder['name']:<12} {folder['timestamp']}")
if(folder['type']=='dir'):
for checksubDir in (folder['files']):#handles sub directories
print_folder(checksubDir, indent+1)
if(folder['type']=='file'):
for s in range(0,indent):#looping for spaces
print(' ')
print_file_listing(folder)
`def main(): root = json_reader.read_json('dir_tree_1.json') 打印文件夹(根)
主() `
【问题讨论】:
-
pprint.pprint听起来像您要找的东西。 -
实际问题是什么? “我正在寻求家庭作业的帮助”不是问题......
标签: python python-3.x dictionary recursion tree