@@ -21,51 +21,33 @@ def count_lines_of_code(file_path):
2121 lines = file .readlines ()
2222 return len (lines )
2323 except Exception as e :
24- # If there is an error reading the file, print the error and skip the file
2524 print (f"Error reading { file_path } : { e } " )
2625 return 0
2726
2827def print_directory_tree (path ):
2928 """Prints the directory structure with files and their line counts."""
3029 folder_files = defaultdict (list )
31- folder_count = 0 # Initialize folder count
32- checked_files = 0 # Initialize checked files count
33-
34- # Process the root directory separately to include files in the root
35- if not os .path .basename (path ).startswith ('.' ): # Skip hidden files and folders
36- folder_name = os .path .basename (path )
37- folder_count += 1 # Increment folder count
38- for file in sorted (os .listdir (path )):
39- file_path = os .path .join (path , file )
40- if os .path .isfile (file_path ) and not file .startswith ('.' ):
41- lines = count_lines_of_code (file_path )
42- if lines > 0 :
43- folder_files [folder_name ].append ((file , lines ))
44- checked_files += 1 # Increment checked files count
30+ folder_count = 0
31+ checked_files = 0
4532
4633 # Walk through the directory and get files with line counts
4734 for root , dirs , files in os .walk (path ):
48- # Skip any directories that start with a dot
35+ # Skip hidden directories
4936 dirs [:] = [d for d in dirs if not d .startswith ('.' )]
5037
51- # Only process the current directory if it does not start with a dot
5238 rel_root = os .path .relpath (root , path )
53- if rel_root .startswith ('.' ):
54- continue
55-
56- folder_name = rel_root if rel_root != '.' else os .path .basename (path )
57- folder_count += 1 # Increment folder count
39+ folder_name = rel_root if rel_root != '.' else 'root' # Show root separately
40+ folder_count += 1
5841
5942 for file in sorted (files ):
60- # Skip files that start with a dot
6143 if file .startswith ('.' ):
6244 continue
6345
6446 file_path = os .path .join (root , file )
6547 lines = count_lines_of_code (file_path )
6648 if lines > 0 :
6749 folder_files [folder_name ].append ((file , lines ))
68- checked_files += 1 # Increment checked files count
50+ checked_files += 1
6951
7052 # Print directory tree with line counts
7153 for folder in sorted (folder_files .keys ()):
@@ -79,29 +61,21 @@ def print_directory_tree(path):
7961def scan_directory (path ):
8062 """Scan the directory and prints summary of files and line counts."""
8163 print (f"\n Scanning directory: { path } " )
82- total_files = 0
8364 total_lines = 0
84- folder_count = 0
85- checked_files = 0
8665
8766 folder_count , checked_files = print_directory_tree (path )
8867
89- # Count total files and lines, including those outside folders
68+ # Count total lines
9069 for root , dirs , files in os .walk (path ):
91- # Skip any directories that start with a dot
9270 dirs [:] = [d for d in dirs if not d .startswith ('.' )]
9371
9472 for file in files :
95- # Skip files that start with a dot
9673 if file .startswith ('.' ):
9774 continue
9875
9976 file_path = os .path .join (root , file )
10077 lines = count_lines_of_code (file_path )
101- if lines > 0 :
102- total_files += 1
103- total_lines += lines
104- checked_files += 1
78+ total_lines += lines
10579
10680 print (f"\n Total files checked: { checked_files } " )
10781 print (f"Total lines of code: { total_lines } " )
0 commit comments