外观
怎么获取文件夹结构并包含里面的文件
2597字约9分钟
技术文档
一、(Window) 使用 cmd
终端,输入
tree
在tree命令后面加入参数 /f
将以层次的结构显示所有文件夹及文件的名称
二、(Linux) 使用 tree
- 安装
tree
apt install tree -y
tree
参数说明
tree: Invalid argument -`e'.
usage: tree [-acdfghilnpqrstuvxACDFJQNSUX] [-L level [-R]] [-H baseHREF]
[-T title] [-o filename] [-P pattern] [-I pattern] [--gitignore]
[--gitfile[=]file] [--matchdirs] [--metafirst] [--ignore-case]
[--nolinks] [--hintro[=]file] [--houtro[=]file] [--inodes] [--device]
[--sort[=]<name>] [--dirsfirst] [--filesfirst] [--filelimit #] [--si]
[--du] [--prune] [--charset[=]X] [--timefmt[=]format] [--fromfile]
[--fromtabfile] [--fflinks] [--info] [--infofile[=]file] [--noreport]
[--version] [--help] [--] [directory ...]
root@cvm-3mj428j2da223:~# tree --help
usage: tree [-acdfghilnpqrstuvxACDFJQNSUX] [-L level [-R]] [-H baseHREF]
[-T title] [-o filename] [-P pattern] [-I pattern] [--gitignore]
[--gitfile[=]file] [--matchdirs] [--metafirst] [--ignore-case]
[--nolinks] [--hintro[=]file] [--houtro[=]file] [--inodes] [--device]
[--sort[=]<name>] [--dirsfirst] [--filesfirst] [--filelimit #] [--si]
[--du] [--prune] [--charset[=]X] [--timefmt[=]format] [--fromfile]
[--fromtabfile] [--fflinks] [--info] [--infofile[=]file] [--noreport]
[--version] [--help] [--] [directory ...]
------- Listing options -------
-a All files are listed.
-d List directories only.
-l Follow symbolic links like directories.
-f Print the full path prefix for each file.
-x Stay on current filesystem only.
-L level Descend only level directories deep.
-R Rerun tree when max dir level reached.
-P pattern List only those files that match the pattern given.
-I pattern Do not list files that match the given pattern.
--gitignore Filter by using .gitignore files.
--gitfile X Explicitly read gitignore file.
--ignore-case Ignore case when pattern matching.
--matchdirs Include directory names in -P pattern matching.
--metafirst Print meta-data at the beginning of each line.
--prune Prune empty directories from the output.
--info Print information about files found in .info files.
--infofile X Explicitly read info file.
--noreport Turn off file/directory count at end of tree listing.
--charset X Use charset X for terminal/HTML and indentation line output.
--filelimit # Do not descend dirs with more than # files in them.
-o filename Output to file instead of stdout.
------- File options -------
-q Print non-printable characters as '?'.
-N Print non-printable characters as is.
-Q Quote filenames with double quotes.
-p Print the protections for each file.
-u Displays file owner or UID number.
-g Displays file group owner or GID number.
-s Print the size in bytes of each file.
-h Print the size in a more human readable way.
--si Like -h, but use in SI units (powers of 1000).
--du Compute size of directories by their contents.
-D Print the date of last modification or (-c) status change.
--timefmt <f> Print and format time according to the format <f>.
-F Appends '/', '=', '*', '@', '|' or '>' as per ls -F.
--inodes Print inode number of each file.
--device Print device ID number to which each file belongs.
------- Sorting options -------
-v Sort files alphanumerically by version.
-t Sort files by last modification time.
-c Sort files by last status change time.
-U Leave files unsorted.
-r Reverse the order of the sort.
--dirsfirst List directories before files (-U disables).
--filesfirst List files before directories (-U disables).
--sort X Select sort: name,version,size,mtime,ctime.
------- Graphics options -------
-i Don't print indentation lines.
-A Print ANSI lines graphic indentation lines.
-S Print with CP437 (console) graphics indentation lines.
-n Turn colorization off always (-C overrides).
-C Turn colorization on always.
------- XML/HTML/JSON options -------
-X Prints out an XML representation of the tree.
-J Prints out an JSON representation of the tree.
-H baseHREF Prints out HTML format with baseHREF as top directory.
-T string Replace the default HTML title and H1 header with string.
--nolinks Turn off hyperlinks in HTML output.
--hintro X Use file X as the HTML intro.
--houtro X Use file X as the HTML outro.
------- Input options -------
--fromfile Reads paths from files (.=stdin)
--fromtabfile Reads trees from tab indented files (.=stdin)
--fflinks Process link information when using --fromfile.
------- Miscellaneous options -------
--version Print version and exit.
--help Print usage and this help message and exit.
-- Options processing terminator.
主要选项解释[1]:
-a:显示所有文件和目录;
-A:使用ASNI绘图字符显示树状图而非以ASCII字符组合;
-C:在文件和目录清单加上色彩,便于区分各种类型;
-d:先是目录名称而非内容;
-D:列出文件或目录的更改时间;
-f:在每个文件或目录之前,显示完整的相对路径名称;
-F:在执行文件,目录,Socket,符号连接,管道名称名称,各自加上"*","/","@","|"号;
-g:列出文件或目录的所属群组名称,没有对应的名称时,则显示群组识别码;
-i:不以阶梯状列出文件和目录名称;
-l:<范本样式> 不显示符号范本样式的文件或目录名称;
-l:如遇到性质为符号连接的目录,直接列出该连接所指向的原始目录;
-n:不在文件和目录清单加上色彩;
-N:直接列出文件和目录名称,包括控制字符;
-p:列出权限标示;
-P:<范本样式> 只显示符合范本样式的文件和目录名称;
-q:用“?”号取代控制字符,列出文件和目录名称;
-s:列出文件和目录大小;
-t:用文件和目录的更改时间排序;
-u:列出文件或目录的拥有者名称,没有对应的名称时,则显示用户识别码;
-x:将范围局限在现行的文件系统中,若指定目录下的某些子目录,其存放于另一个文件系统上,则将该目录予以排除在寻找范围外。
三、通过 Python
实现
获取一个文件夹的结构并包含里面的文件信息可以通过编程实现。以下是一个使用 Python
获取文件夹结构的示例:
1. 初步实现
方法 1:使用 os
模块
import os
def get_folder_structure(root_folder):
folder_structure = {}
for root, dirs, files in os.walk(root_folder):
# 获取相对于根文件夹的路径
relative_path = os.path.relpath(root, root_folder)
if relative_path == ".":
relative_path = root_folder
folder_structure[relative_path] = {"dirs": dirs, "files": files}
return folder_structure
# 示例:获取当前目录的结构
root_dir = "你的目标文件夹路径"
structure = get_folder_structure(root_dir)
# 打印结果
import pprint
pprint.pprint(structure)
方法 2:使用 pathlib
模块(Python 3.5+)
from pathlib import Path
def get_folder_structure_v2(root_folder):
root_path = Path(root_folder)
folder_structure = {}
for path in root_path.rglob('*'):
relative_path = path.relative_to(root_path).parent
folder_key = str(relative_path) if relative_path != Path('.') else str(root_path)
if folder_key not in folder_structure:
folder_structure[folder_key] = {"dirs": [], "files": []}
if path.is_dir():
folder_structure[folder_key]["dirs"].append(path.name)
else:
folder_structure[folder_key]["files"].append(path.name)
return folder_structure
# 示例:获取当前目录的结构
root_dir = "你的目标文件夹路径"
structure = get_folder_structure_v2(root_dir)
# 打印结果
import pprint
pprint.pprint(structure)
输出示例
假设文件夹结构如下:
example_folder/
├── file1.txt
├── file2.txt
└── subfolder/
└── file3.txt
程序输出:
{
'example_folder': {
'dirs': ['subfolder'],
'files': ['file1.txt', 'file2.txt']
},
'example_folder/subfolder': {
'dirs': [],
'files': ['file3.txt']
}
}
保存为文件(可选)
如果需要将结构保存到 JSON 文件中,可以用以下代码:
import json
with open("folder_structure.json", "w", encoding="utf-8") as f:
json.dump(structure, f, ensure_ascii=False, indent=4)
2. 输出树状图
当然可以输出树状图!可以用 Python 实现一个简单的树状图结构来展示文件夹和文件。以下是示例代码:
方法:使用递归输出树状图
import os
def print_tree(folder_path, indent=""):
items = os.listdir(folder_path)
for index, item in enumerate(items):
item_path = os.path.join(folder_path, item)
is_last = index == len(items) - 1
# 使用不同符号表示文件和文件夹
prefix = "└── " if is_last else "├── "
print(indent + prefix + item)
# 如果是文件夹,递归调用
if os.path.isdir(item_path):
new_indent = indent + (" " if is_last else "│ ")
print_tree(item_path, new_indent)
# 示例:打印当前目录的树状结构
root_dir = "你的目标文件夹路径"
print(f"{root_dir}/")
print_tree(root_dir)
示例输出
假设目标文件夹结构为:
example_folder/
├── file1.txt
├── file2.txt
└── subfolder/
├── file3.txt
└── subsubfolder/
└── file4.txt
运行代码后会输出:
example_folder/
├── file1.txt
├── file2.txt
└── subfolder/
├── file3.txt
└── subsubfolder/
└── file4.txt
优化:用 rich
库实现高亮树状图(推荐)
rich
是一个强大的库,可以轻松实现美观的树状结构输出。
安装 rich
:
pip install rich
使用 rich
输出树状图:
from rich.tree import Tree
from rich.console import Console
import os
def build_tree(folder_path, tree):
for item in os.listdir(folder_path):
item_path = os.path.join(folder_path, item)
if os.path.isdir(item_path):
branch = tree.add(item)
build_tree(item_path, branch)
else:
tree.add(item)
def print_tree_with_rich(folder_path):
console = Console()
tree = Tree(folder_path)
build_tree(folder_path, tree)
console.print(tree)
# 示例:打印目标文件夹的树状结构
root_dir = "你的目标文件夹路径"
print_tree_with_rich(root_dir)
示例输出(带颜色):
example_folder
├── file1.txt
├── file2.txt
└── subfolder
├── file3.txt
└── subsubfolder
└── file4.txt
总结
- 普通版本:使用
os
模块递归输出简单的树状图。 - 增强版本:用
rich
库输出更美观的高亮树状图。
3. 改进代码,使其选择性输出
要实现选择性输出文件夹和文件,我们可以通过指定规则过滤某些文件夹或文件。以下是实现方法:
改进代码:普通树状图(排除指定文件夹或文件)
import os
def print_tree(folder_path, indent="", exclude_dirs=None, exclude_files=None):
"""
打印文件夹树状图,并支持排除特定文件夹或文件。
:param folder_path: 目标文件夹路径
:param indent: 缩进,用于递归
:param exclude_dirs: 要排除的文件夹列表
:param exclude_files: 要排除的文件列表
"""
exclude_dirs = exclude_dirs or []
exclude_files = exclude_files or []
items = os.listdir(folder_path)
items.sort() # 按名称排序,方便查看
for index, item in enumerate(items):
item_path = os.path.join(folder_path, item)
# 跳过指定的文件夹和文件
if os.path.isdir(item_path) and item in exclude_dirs:
continue
if os.path.isfile(item_path) and item in exclude_files:
continue
# 判断是否是最后一个元素
is_last = index == len(items) - 1
prefix = "└── " if is_last else "├── "
print(indent + prefix + item)
# 如果是文件夹,递归调用
if os.path.isdir(item_path):
new_indent = indent + (" " if is_last else "│ ")
print_tree(item_path, new_indent, exclude_dirs, exclude_files)
# 示例:打印树状结构,排除特定文件夹或文件
root_dir = "你的目标文件夹路径"
exclude_dirs = ["li"] # 排除的文件夹
exclude_files = [] # 排除的文件
print(f"{root_dir}/")
print_tree(root_dir, exclude_dirs=exclude_dirs, exclude_files=exclude_files)
增强版:带 rich
的高亮树状图(支持排除规则)
方法:
我们可以在构建树的时候,判断当前节点是否在排除规则中,如果是,则跳过。
from rich.tree import Tree
from rich.console import Console
import os
def build_tree_with_exclude(folder_path, tree, exclude_dirs=None, exclude_files=None):
"""
构建文件夹树状图,并支持排除特定文件夹或文件。
:param folder_path: 目标文件夹路径
:param tree: rich 的树节点
:param exclude_dirs: 要排除的文件夹列表
:param exclude_files: 要排除的文件列表
"""
exclude_dirs = exclude_dirs or []
exclude_files = exclude_files or []
items = os.listdir(folder_path)
items.sort() # 按名称排序,方便查看
for item in items:
item_path = os.path.join(folder_path, item)
# 跳过指定的文件夹或文件
if os.path.isdir(item_path) and item in exclude_dirs:
continue
if os.path.isfile(item_path) and item in exclude_files:
continue
if os.path.isdir(item_path):
branch = tree.add(f"📂 {item}")
build_tree_with_exclude(item_path, branch, exclude_dirs, exclude_files)
else:
tree.add(f"📄 {item}")
def print_tree_with_rich_and_exclude(folder_path, exclude_dirs=None, exclude_files=None):
"""
打印文件夹树状图(带高亮),支持排除特定文件夹或文件。
:param folder_path: 目标文件夹路径
:param exclude_dirs: 要排除的文件夹列表
:param exclude_files: 要排除的文件列表
"""
console = Console()
tree = Tree(f"[bold magenta]{folder_path}[/bold magenta]")
build_tree_with_exclude(folder_path, tree, exclude_dirs, exclude_files)
console.print(tree)
# 示例:打印树状结构,排除特定文件夹或文件
root_dir = "你的目标文件夹路径"
exclude_dirs = ["li"] # 排除的文件夹
exclude_files = ["file1.txt"] # 排除的文件
print_tree_with_rich_and_exclude(root_dir, exclude_dirs, exclude_files)
示例
假设目录结构:
example_folder/
├── file1.txt
├── file2.txt
├── ex/
│ ├── file3.txt
│ └── li/
│ └── file4.txt
└── other/
└── file5.txt
运行排除规则:
exclude_dirs = ["li"]
exclude_files = ["file1.txt"]
普通输出:
example_folder/
├── file2.txt
├── ex/
│ └── file3.txt
└── other/
└── file5.txt
Rich 输出(带高亮):
📂 example_folder
├── 📄 file2.txt
├── 📂 ex
│ └── 📄 file3.txt
└── 📂 other
└── 📄 file5.txt
可扩展性
可以根据需要调整 exclude_dirs
和 exclude_files
列表,灵活控制要排除的内容。是否很方便呢? 🎉