[Solved]-Count lines of code in a Django Project

19👍

Yep:

shell]$ find /my/source -name "*.py" -type f -exec cat {} + | wc -l

Job’s a good ‘un.

8👍

You might want to look at CLOC — it’s not Django specific but it supports Python. It can show you lines counts for actual code, comments, blank lines, etc.

4👍

Starting with Aiden’s answer, and with a bit of help in a question of my own, I ended up with this god-awful mess:

# find the combined LOC of files
# usage: loc Documents/fourU py html
function loc {
    #find $1 -name $2 -type f -exec cat {} + | wc -l
    namelist=''
    let i=2
    while [ $i -le $# ]; do
        namelist="$namelist -name \"*.$@[$i]\""
        if [ $i != $# ]; then
            namelist="$namelist -or "
        fi
        let i=i+1
    done
    #echo $namelist
    #echo "find $1 $namelist" | sh
    #echo "find $1 $namelist" | sh | xargs cat
    echo "find $1 $namelist" | sh | xargs cat | wc -l
}

which allows you to specify any number of extensions you want to match. As far as I can tell, it outputs the right answer, but… I thought this would be a one-liner, else I wouldn’t have started in bash, and it just kinda grew from there.

I’m sure that those more knowledgable than I can improve upon this, so I’m going to put it in community wiki.

1👍

Check out the wc command on unix.

0👍

Get wc command on Windows using GnuWin32 (http://gnuwin32.sourceforge.net/packages/coreutils.htm)

wc *.py

Leave a comment