python工程目录树管理

#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中去就可快速获取到工程目录情况,减少手动操作

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值