#python工程管理#chatgpt3#
利用windows和linux的目录树tree在对应目录下生成目录树
详情自行百度 tree 命名
# windows下目录执行 ,tree -f 可遍历当前目录下的所包含所有文件情况
tree -f
#linux同理
tree
windows下通过tree -f >list.txt
使用以下python脚本可获取py文件中的函数名,类名,及函数与类的层级关系
import ast
class ClassFunctionVisitor(ast.NodeVisitor):
def __init__(self):
self.tree = []
self.current_class = None
def visit_ClassDef(self, node):
class_info = {
'name': node.name,
'type': 'ClassDef',
'children': []
}
self.current_class = class_info
self.tree.append(class_info)
for item in node.body:
if isinstance(item, ast.FunctionDef):
func_info = {
'name': item.name,
'type': 'FunctionDef',
'children': []
}
class_info['children'].append(func_info)
self.generic_visit(item)
def visit_FunctionDef(self, node):
func_info = {
'name': node.name,
'type': 'FunctionDef',
'children': []
}
self.tree.append(func_info)
# 递归构建目录树
def build_tree(node, indent=''):
result = ''
for item in node:
result += f'{indent}+ {item["name"]} -- {item["type"]} \n'
if 'children' in item:
result += build_tree(item['children'], indent + ' ')
return result
def get_classes_and_functions(file_path):
with open(file_path, 'r',encoding='utf-8') as file:
source_code = file.read()
tree = ast.parse(source_code)
visitor = ClassFunctionVisitor()
visitor.visit(tree)
return visitor.tree
# 示例1的Python代码
file_path1 = 'yourPythonPath'
tree1 = get_classes_and_functions(file_path1)
directory_tree1 = build_tree(tree1)
print("示例1的目录树:")
print(directory_tree1)
将结果复制到excel中去就可快速获取到工程目录情况,减少手动操作