python count_lines_in_dir.py

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python count_lines_in_dir.py相关的知识,希望对你有一定的参考价值。

#!/usr/bin/python
"""
Count the number of lines in files recursively in a given directory.

To run direct rather than from the command line, switch the last line around.
"""


import os
import sys


def count_lines(f):
    counter = 0
    f = open(f, "r")
    for line in f.read().split('\n'):
        counter += 1
    f.close()
    return counter


def count_dir(dirname):
    counter = 0
    for f in os.listdir(dirname):
        fa = os.path.join(dirname, f)
        if os.path.isdir(fa):
            dcount = count_dir(fa)
            counter = counter + dcount
        else:
            fcount = count_lines(fa)
            counter = counter + fcount
    return counter


#print count_dir(r"C:\stuff")
print count_dir(sys.argv[1])

以上是关于python count_lines_in_dir.py的主要内容,如果未能解决你的问题,请参考以下文章

001--python全栈--基础知识--python安装

Python代写,Python作业代写,代写Python,代做Python

Python开发

Python,python,python

Python 介绍

Python学习之认识python