广州双城热恋创意文化传播有限公司

现在的位置: 主页 > 新闻中心 > 文章列表

文章正文

Python-统计特定文件夹中,特定文件代码行数的小程序

作者:广州双城热恋创意文化传播有限公司 来源:www.999loveyou.com 发布时间:2017-09-02 11:45:42
Python:统计特定文件夹中,特定文件代码行数的小程序 最近自己在帮朋友做一个外贸管理系统项目,哼哧哼哧弄了2个月,写了一大堆的java代码,还有xhtml文件,处于对自己成就感满足的需求,打算写个程序统计一下目前总共的代码行数,正好最近在学习Python程序,于是实现的语言选择了Python,由于自己是新手,所以写的不足的地方还请指点

程序如下(统计特定目录下,java文件的行数):

[python]

'''''

count file lines number by directory

@author: aihua.sun

'''

import os.path;

# -*- coding: utf-8 -*-

import os;

import string;

import codecs;

resultFile = open("result.file", "w+");

DOC_TAG = "'''";

FILE_COUNT = 0;

FILE_EXTENSION = ".java";

LINE_NUMBER = 0;

COUNT_LINE_NUMBER = True;

DEFAULT_DIR = "D:\sourcecode\FTS";

def collection():

dirpath = input("please input module directionary:");

if os.path.isdir(dirpath):

walkTree(dirpath);

else:

print("please input a directionary path!");

#use default directory

dirpath = DEFAULT_DIR;

walkTree(dirpath);

print("DIRECTOTY:%s FILE_EXTENSION:%s LINE_NUMBER:%s" % (dirpath, FILE_EXTENSION, LINE_NUMBER))

def walkTree(dirpath):

os.chdir(dirpath);

fileExtensionLen = len(FILE_EXTENSION);

for subFile in os.listdir(dirpath):

#print(os.getcwd());

if os.path.isdir(subFile):

fullSubFile = str(os.getcwd()) + str(os.sep) + subFile;

#print(fullSubFile);

walkTree(fullSubFile);

elif os.path.basename(subFile)[-fileExtensionLen:] == FILE_EXTENSION:

collectDocument(subFile);

os.chdir(os.path.pardir);

def collectDocument(subFile):

countDirectoryLineNumber(subFile)

resultFile.writelines("\n" + "FILE: %s%s%s" % (str(os.getcwd()), str(os.sep), str(os.path.basename(subFile))));

resultFile.writelines("\n" + "DOCUMENT:");

appendDocToResultFile(subFile, resultFile);

def countDirectoryLineNumber(subFile):

#print(subFile)

global LINE_NUMBER;

#use "w+" will write clear target file, so it's dangerous

open = codecs.open;

newFileObject = open(subFile, "r+", 'utf-8');

singleFileLineNumber = len(newFileObject.readlines());

#print(singleFileLineNumber);

LINE_NUMBER += singleFileLineNumber;

def appendDocToResultFile(subFile, resultFile):

inDocArea = False;

for fileLine in subFile:

#begin with document

if fileLine.find(DOC_TAG) > 0:

inDocArea = True;

if inDocArea:

resultFile.writelines(fileLine);

#end of document

if inDocArea and fileLine.find(DOC_TAG) > 0:

resultFile.writelines(fileLine);

resultFile.close();

break;

if __name__ == '__main__':

collection();

'''

count file lines number by directory

@author: aihua.sun

'''

import os.path;

# -*- coding: utf-8 -*-

import os;

import string;

import codecs;

resultFile = open("result.file", "w+");

DOC_TAG = "'''";

FILE_COUNT = 0;

FILE_EXTENSION = ".java";

LINE_NUMBER = 0;

COUNT_LINE_NUMBER = True;

DEFAULT_DIR = "D:\sourcecode\FTS";

def collection():

dirpath = input("please input module directionary:");

if os.path.isdir(dirpath):

walkTree(dirpath);

else:

print("please input a directionary path!");

#use default directory

dirpath = DEFAULT_DIR;

walkTree(dirpath);

print("DIRECTOTY:%s FILE_EXTENSION:%s LINE_NUMBER:%s" % (dirpath, FILE_EXTENSION, LINE_NUMBER))

def walkTree(dirpath):

os.chdir(dirpath);

fileExtensionLen = len(FILE_EXTENSION);

for subFile in os.listdir(dirpath):

#print(os.getcwd());

if os.path.isdir(subFile):

fullSubFile = str(os.getcwd()) + str(os.sep) + subFile;

#print(fullSubFile);

walkTree(fullSubFile);

elif os.path.basename(subFile)[-fileExtensionLen:] == FILE_EXTENSION:

collectDocument(subFile);

os.chdir(os.path.pardir);

def collectDocument(subFile):

countDirectoryLineNumber(subFile)

resultFile.writelines("\n" + "FILE: %s%s%s" % (str(os.getcwd()), str(os.sep), str(os.path.basename(subFile))));

resultFile.writelines("\n" + "DOCUMENT:");

appendDocToResultFile(subFile, resultFile);

def countDirectoryLineNumber(subFile):

#print(subFile)

global LINE_NUMBER;

#use "w+" will write clear target file, so it's dangerous

open = codecs.open;

newFileObject = open(subFile, "r+", 'utf-8');

singleFileLineNumber = len(newFileObject.readlines());

#print(singleFileLineNumber);

LINE_NUMBER += singleFileLineNumber;

def appendDocToResultFile(subFile, resultFile):

inDocArea = False;www.2cto.com

for fileLine in subFile:

#begin with document

if fileLine.find(DOC_TAG) > 0:

inDocArea = True;

if inDocArea:

resultFile.writelines(fileLine);

#end of document

if inDocArea and fileLine.find(DOC_TAG) > 0:

resultFile.writelines(fileLine);

resultFile.close();

break;

if __name__ == '__main__':

collection();

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:荆州SEO http://jingzhou.raoyu.net

COPYRIGHT © 2015 广州双城热恋创意文化传播有限公司 ALL RIGHTS RESERVED. 网站地图 技术支持:肥猫科技
精彩专题:网站建设
购买本站友情链接、项目合作请联系客服QQ:2500-38-100