|
| 1 | +import os |
| 2 | + |
| 3 | +def count_lines_in_file(file_path): |
| 4 | + """Counts the number of lines in a given file.""" |
| 5 | + with open(file_path, 'r', encoding='utf-8') as file: |
| 6 | + return sum(1 for _ in file) |
| 7 | + |
| 8 | +def read_file_content(file_path): |
| 9 | + """Reads and returns the content of a file.""" |
| 10 | + with open(file_path, 'r', encoding='utf-8') as file: |
| 11 | + return file.read() |
| 12 | + |
| 13 | +def scan_java_files(directory): |
| 14 | + """Scans Java files in a directory and filters those with over 100 lines.""" |
| 15 | + java_files_over_100_lines = [] |
| 16 | + |
| 17 | + for root, _, files in os.walk(directory): |
| 18 | + for file in files: |
| 19 | + if file.endswith(".java"): |
| 20 | + file_path = os.path.join(root, file) |
| 21 | + line_count = count_lines_in_file(file_path) |
| 22 | + if line_count > 100: |
| 23 | + content = read_file_content(file_path) |
| 24 | + java_files_over_100_lines.append((file, line_count, content)) |
| 25 | + |
| 26 | + return java_files_over_100_lines |
| 27 | + |
| 28 | +if __name__ == "__main__": |
| 29 | + directory = "../solutions/fourththousand" |
| 30 | + file_name = directory.split("/")[-1] |
| 31 | + result = scan_java_files(directory) |
| 32 | + |
| 33 | + if result: |
| 34 | + print("Java files with more than 100 lines:") |
| 35 | + for file, lines, _ in result: |
| 36 | + print(f"{file}: {lines} lines") |
| 37 | + with open(f"./{file_name}.txt", "w", encoding='utf-8') as f: |
| 38 | + f.write(f"Total files count: {len(result)}\n") |
| 39 | + for file, lines, content in result: |
| 40 | + f.write(f"{'='*50}\n") |
| 41 | + f.write(f"File: {file}\n") |
| 42 | + f.write(f"Line count: {lines}\n") |
| 43 | + f.write(f"{'='*50}\n") |
| 44 | + f.write("Content:\n") |
| 45 | + f.write(content) |
| 46 | + f.write("\n\n") |
| 47 | + else: |
| 48 | + print("No Java files with more than 100 lines found.") |
0 commit comments