'Counting lines of code in a Django project

I use this shell script to count the lines of code in a Django project,

find . -name "*.py" -type f -exec grep . {} \; | wc -l

How can I modify this to not count the migration scripts? Essentially that means not count anything inside any subfolder by the name migrations.



Solution 1:[1]

Given you want to exclude all files in a directory named migrations, you can add a condition to the find command:

find . -name "*.py" -type f ! -path '*/migrations/*' -exec grep . {} \; | wc -l

Note that counting the number of lines is probably easier with cloc [GitHub]:

cloc --not-match-d=migrations .

This will then generate a summary like:

$ cloc --not-match-d='migrations' .
     102 text files.
      86 unique files.                              
      38 files ignored.

github.com/AlDanial/cloc v 1.74  T=0.88 s (97.3 files/s, 42411.8 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
XML                              2             11             32          33299
Python                          63            619            273           2673
HTML                            18             35             49            304
JavaScript                       2              8              5            100
CSS                              1              5              6             81
-------------------------------------------------------------------------------
SUM:                            86            678            365          36457
-------------------------------------------------------------------------------

Solution 2:[2]

I have seen many solutions but this is probably the easiest, If you just want something simple.

files = [YOUR FILES HERE]

for file in files:
    with open(file) as f:
        print(len(file.read_lines()))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Irtiza Babar