在 Python 或 Bash 中计算代码行数的实用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7507846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Utility To Count Number Of Lines Of Code In Python Or Bash
提问by Justin
Is there a quick and dirty way in either python or bash script, that can recursively descend a directory and count the total number of lines of code? We would like to be able to exclude certain directories though.
在 python 或 bash 脚本中是否有一种快速而肮脏的方法,可以递归地下降目录并计算代码的总行数?不过,我们希望能够排除某些目录。
For example:
例如:
start at: /apps/projects/reallycoolapp
exclude: lib/, frameworks/
The excluded directories should be recursive as well. For example:
排除的目录也应该是递归的。例如:
/app/projects/reallycool/lib SHOULD BE EXCLUDED
/app/projects/reallycool/modules/apple/frameworks SHOULD ALSO BE EXCLUDED
This would be a really useful utility.
这将是一个非常有用的实用程序。
回答by Justin
Found an awesome utility CLOC. https://github.com/AlDanial/cloc
发现了一个很棒的实用程序 CLOC。https://github.com/AlDanial/cloc
Here is the command we ran:
这是我们运行的命令:
perl cloc.pl /apps/projects/reallycoolapp --exclude-dir=lib,frameworks
And here is the output
这是输出
--------------------------------------------------------------------------------
Language files blank comment code
--------------------------------------------------------------------------------
PHP 32 962 1352 2609
Javascript 5 176 225 920
Bourne Again Shell 4 45 70 182
Bourne Shell 12 52 113 178
HTML 1 0 0 25
--------------------------------------------------------------------------------
SUM: 54 1235 1760 3914
--------------------------------------------------------------------------------
回答by Lynch
The findand wcarguments alone can solve your problem.
在find和wc参数单独可以解决你的问题。
With findyou can specify very complex logic like this:
随着find您可以指定这样的非常复杂的逻辑:
find /apps/projects/reallycoolapp -type f -iname '*.py' ! -path '*/lib/*' ! -path '*/frameworks/*' | xargs wc -l
Here the !invert the condition so this command will count the lines for each python files not in 'lib/' or in 'frameworks/' directories.
这里!反转条件,因此此命令将计算不在“lib/”或“frameworks/”目录中的每个python文件的行数。
Just dont forget the '*' or it will not match anything.
只是不要忘记 '*' 否则它不会匹配任何东西。
回答by mlathe
find ./apps/projects/reallycool -type f | \
grep -v -e /app/projects/reallycool/lib \
-e /app/projects/reallycool/modules/apple/frameworks | \
xargs wc -l | \
cut -d '.' -f 1 | \
awk 'BEGIN{total=0} {total += } END{print total}'
A few notes...
一些注意事项...
- the . after the find is important since that's how the
cutcommand can separate the count from the file name - this is a multiline command, so make sure there aren't spaces after the escaping slashes
- you might need to exclude other files like svn or something. Also this will give funny values for binary files so you might want to use grep to whitelist the specific file types you are interested in, ie:
grep -e .html$ -e .css$
- 这 。在 find 之后很重要,因为这就是
cut命令可以将计数与文件名分开的方式 - 这是一个多行命令,因此请确保转义斜杠后没有空格
- 您可能需要排除其他文件,例如 svn 或其他文件。此外,这将为二进制文件提供有趣的值,因此您可能希望使用 grep 将您感兴趣的特定文件类型列入白名单,即:
grep -e .html$ -e .css$

