[原创]Python小工具 —— 计算 文件 或 文件夹及其所有子文件夹中所有文件 的 代码行数
2012-08-31 08:36
976 查看
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Copyright (c) 2012 - Yang Zhao <yangzhao3d@gmail.com>
import sys, codecs, os, traceback
print 'Count Code Lines of Files In Current File or Folder with All It\'s Subfolders'
print 'Copyright (c) 2012 - Yang Zhao <yangzhao3d@gmail.com>\n'
all_line_count = 0
file_count = 0
def process_file(path):
global all_line_count, file_count
file_count = file_count + 1
current_line_count = 0
with open(path, 'r') as fr:
for line in fr:
all_line_count = all_line_count + 1
current_line_count = current_line_count + 1
#print '%d. File: %s, lines: %d' % (file_count, path, current_line_count)
def process_para(path):
if not os.path.isdir(path) and not os.path.isfile(path):
return False
if os.path.isfile(path):
process_file(path)
else:
for item in os.listdir(path): # process the whole directory
subpath = os.path.join(path, item)
process_para(subpath)
def print_usage():
print 'Usage:\n%s [input file or directory]' % os.path.basename(sys.argv[0])
try:
process_para(sys.argv[1])
print '-- All File Count: %d, lines: %d --' % (file_count, all_line_count)
except:
print_usage()
print traceback.print_exc()
sys.exit(1)
# -*- encoding: utf-8 -*-
# Copyright (c) 2012 - Yang Zhao <yangzhao3d@gmail.com>
import sys, codecs, os, traceback
print 'Count Code Lines of Files In Current File or Folder with All It\'s Subfolders'
print 'Copyright (c) 2012 - Yang Zhao <yangzhao3d@gmail.com>\n'
all_line_count = 0
file_count = 0
def process_file(path):
global all_line_count, file_count
file_count = file_count + 1
current_line_count = 0
with open(path, 'r') as fr:
for line in fr:
all_line_count = all_line_count + 1
current_line_count = current_line_count + 1
#print '%d. File: %s, lines: %d' % (file_count, path, current_line_count)
def process_para(path):
if not os.path.isdir(path) and not os.path.isfile(path):
return False
if os.path.isfile(path):
process_file(path)
else:
for item in os.listdir(path): # process the whole directory
subpath = os.path.join(path, item)
process_para(subpath)
def print_usage():
print 'Usage:\n%s [input file or directory]' % os.path.basename(sys.argv[0])
try:
process_para(sys.argv[1])
print '-- All File Count: %d, lines: %d --' % (file_count, all_line_count)
except:
print_usage()
print traceback.print_exc()
sys.exit(1)
相关文章推荐
- python3 计算文件夹中所有py文件里面代码行数,注释行数,空行数
- C++ 一个统计文件夹下所有代码文件行数的小工具
- 使用某个文件夹下的所有文件去替换另一个文件夹下及其子文件夹下存在的同名文件(Python实现)
- 遍历文件夹及其子文件夹下所有文件
- 非递归遍历指定文件夹下的所有文件及其子文件夹
- 列出当前文件夹及其子文件夹下所有文件名称
- 文件基本操作之获取文件夹下的所有子文件夹及其名称
- python 脚本(获取指定文件夹、指定文件格式、的代码行数、注释行数)
- python 3.3 复制文件 或 文件夹及其子文件夹 小例
- php 获取某个文件夹及其子文件夹的所有文件并支持文件格式的筛选
- Java 代码删除文件夹及其子文件夹和文件操作
- 【Python 写的小工具】计算 txt 文件中中文字符的平均字数和平均行数
- [原创]DES加密解密工具2.1及其代码——支持字符串及文件加密,支持3重DES
- php遍历文件夹及其下所有文件的代码
- 取出文件夹及其所有子文件夹中所有文件的方法
- python 脚本(获取指定文件夹、指定文件格式、的代码行数、注释行数)
- python 查找文件夹下所有文件 实现代码
- Python列出一个文件夹及其子目录的所有文件
- python列举文件夹下所有文件行数
- python--统计某个路径下,文件的种类及其行数(包括子文件夹内的文件)