如何修改Python脚本遍历多文件夹提取resources.json指定字段
Solution for Traversing Multiple Folders to Generate Lambda Tag Compliance Reports
Hey there, let's tweak your existing script to process all 8 of your target folders (each containing a resources.json file) and generate a structured compliance report. Here's the updated code with practical improvements:
#!/usr/bin/python import os import json # Root directory containing all your target folders rootdir = "output" # Path to save the final compliance report report_path = "lambda_tag_compliance_report.txt" # Open the report file for writing (overwrites existing file if present) with open(report_path, "w") as report_file: # Walk through every directory and file under the root for subdir, dirs, files in os.walk(rootdir): for file in files: # Build the full file path safely (works across all OS) filepath = os.path.join(subdir, file) # Target only resources.json files if filepath.endswith("resources.json"): print(f"Processing: {filepath}") try: # Load and parse the JSON file with open(filepath) as json_file: data = json.load(json_file) # Iterate through each Lambda function entry in the JSON for function_entry in data: # Safely extract fields (default to "N/A" if missing) func_name = function_entry.get("FunctionName", "N/A") func_arn = function_entry.get("FunctionArn", "N/A") # Format the line for the report report_line = f"FunctionName: {func_name} | FunctionArn: {func_arn}\n" # Write to report and print to console report_file.write(report_line) print(report_line.strip()) except json.JSONDecodeError: error_msg = f"⚠️ Error: Invalid JSON in {filepath}\n" report_file.write(error_msg) print(error_msg.strip()) except KeyError as e: error_msg = f"⚠️ Error: Missing field '{str(e)}' in {filepath}\n" report_file.write(error_msg) print(error_msg.strip()) except Exception as e: error_msg = f"⚠️ Unexpected error processing {filepath}: {str(e)}\n" report_file.write(error_msg) print(error_msg.strip()) print(f"\n✅ Compliance report saved to: {report_path}")
Key Changes Explained:
- Removed folder name restriction: The original script only processed files under
lambda-tag-compliancefolders. Now it targets everyresources.jsonin theoutputdirectory, perfect for your 8 folders. - Cross-platform path handling: Used
os.path.join()instead of manual path concatenation to ensure the script works on Windows, macOS, and Linux. - Persistent report output: Results are written to
lambda_tag_compliance_report.txtso you have a saved record instead of just console output. - Robust error handling: Catches invalid JSON, missing fields, and unexpected errors—so the script keeps running even if one file has issues, and logs errors to the report.
- Safe field extraction: Uses
dict.get()to avoid crashes if a JSON entry is missingFunctionNameorFunctionArn(defaults to "N/A").
内容的提问来源于stack exchange,提问作者greghaines
相关产品推荐
相关产品推荐

