Python统计计算文件夹下所有代码文件的代码行数

import os

def count_lines_in_file(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        lines = file.readlines()
        return len(lines)

def count_lines_in_folder(folder_path):
    total_lines = 0

    for root, dirs, files in os.walk(folder_path):
        for file in files:
            if file.endswith('.py'):  # 仅统计.py文件
                file_path = os.path.join(root, file)
                lines = count_lines_in_file(file_path)
                total_lines += lines

    return total_lines

def count_lines_recursive(folder_path):
    total_lines = 0

    for root, dirs, files in os.walk(folder_path):
        for dir in dirs:
            dir_path = os.path.join(root, dir)
            lines = count_lines_in_folder(dir_path)
            total_lines += lines

    return total_lines

folder_path = './'  # 替换为要统计的文件夹路径
total_lines = count_lines_recursive(folder_path)
print(f"Total lines of code in the folder: {total_lines}")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Python计算Azure DevOps代码行数的示例代码: ```python import requests # Replace with your organization and project details org_name = "your-org-name" project_name = "your-project-name" # Replace with your personal access token token = "your-personal-access-token" # API endpoint to retrieve code statistics api_endpoint = f"https://dev.azure.com/{org_name}/{project_name}/_apis/git/repositories?api-version=6.0" # HTTP request header to include the authorization token headers = {"Authorization": f"Basic {token}"} # Make HTTP request to retrieve code statistics response = requests.get(api_endpoint, headers=headers) # Check if HTTP request was successful if response.status_code == 200: # Parse JSON response data = response.json() # Initialize line count to zero line_count = 0 # Loop through each repository and retrieve code statistics for repo in data["value"]: repo_id = repo["id"] repo_api_endpoint = f"https://dev.azure.com/{org_name}/{project_name}/_apis/git/repositories/{repo_id}/stats/branches?api-version=6.0" repo_response = requests.get(repo_api_endpoint, headers=headers) repo_data = repo_response.json() repo_line_count = repo_data["total"] line_count += repo_line_count # Print total line count print(f"Total line count: {line_count}") else: # Handle HTTP request error print(f"HTTP request error: {response.status_code} - {response.text}") ``` 在上面的代码中,我们使用了Python的`requests`库来进行HTTP请求,以检索Azure DevOps代码库的代码行数。我们使用Azure DevOps的REST API来检索代码统计信息。您需要将代码中的组织名、项目名和个人访问令牌替换为自己的信息。运行代码后,将输出代码库的总行数

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值