60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
import os
|
||
import datetime
|
||
from collections import defaultdict
|
||
|
||
def build_tree(paths):
|
||
"""将相对路径列表转换为树形字典"""
|
||
tree = lambda: defaultdict(tree)
|
||
root = tree()
|
||
for path in paths:
|
||
parts = path.split(os.sep)
|
||
current = root
|
||
for part in parts:
|
||
current = current[part]
|
||
return root
|
||
|
||
def print_tree(d, indent=""):
|
||
"""递归打印树结构"""
|
||
last_key = list(d.keys())[-1] if d else None
|
||
for i, key in enumerate(d):
|
||
connector = "└── " if key == last_key else "├── "
|
||
print(indent + connector + key)
|
||
new_indent = indent + (" " if key == last_key else "│ ")
|
||
print_tree(d[key], new_indent)
|
||
|
||
def main():
|
||
# 输入目录
|
||
dir_path = input("请输入要检查的目录路径: ").strip()
|
||
if not os.path.isdir(dir_path):
|
||
print("输入的路径无效,请确认是一个目录。")
|
||
return
|
||
|
||
# 输入前缀
|
||
today = datetime.date.today().strftime("%Y%m%d")
|
||
pp = f"AI-{today}-"
|
||
prefix = input(f"请输入文件名前缀(留空则使用 {pp}): ").strip()
|
||
if not prefix:
|
||
prefix = pp
|
||
|
||
print(f"\n使用的前缀是: {prefix}\n")
|
||
|
||
# 遍历目录
|
||
not_matching_files = []
|
||
for root, _, files in os.walk(dir_path):
|
||
for f in files:
|
||
if not f.startswith(prefix):
|
||
full_path = os.path.join(root, f)
|
||
rel_path = os.path.relpath(full_path, dir_path) # 相对路径
|
||
not_matching_files.append(rel_path)
|
||
|
||
# 输出结果
|
||
if not_matching_files:
|
||
print("以下文件不符合命名前缀要求:\n")
|
||
tree = build_tree(not_matching_files)
|
||
print_tree(tree)
|
||
else:
|
||
print("✅ 所有文件名都符合前缀要求。")
|
||
|
||
if __name__ == "__main__":
|
||
main()
|