[d]python如何统计所有文本文件的行数呢?

aa800629 2012-07-27 11:59:22
python如何统计所有文本文件的行数呢?
比如一个目录下,所有 *.frm,*.bas 的文件的行数总和呢?


我的目的是统计代码的行数?

--------------------------
Double行动:
原帖分数:25
帖子加分:25
...全文
868 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq120848369 2012-08-31
  • 打赏
  • 举报
回复
glob+readlines即可
君子美玉 2012-08-30
  • 打赏
  • 举报
回复
字符串有个splitlines的方法,可以试试
zhaoqfeng 2012-08-29
  • 打赏
  • 举报
回复

import os
import os.path
import re

def countfiles(filename):
num = 0
if os.path.isfile(filename):
if not re.match('\S+[\.frm|\.bas]', filename):
return 0
for (i, line) in enumerate(open(filename)):
pass
print ("%s : %d lines" %(filename, i))
return i
for f in os.listdir(filename):
num += countfiles(os.path.join(filename, f))
return num
bugs2k 2012-07-30
  • 打赏
  • 举报
回复
python 2.7 下貌似可以运行:

#!/usr/bin/env python

import os

exts = ('.bas', '.frm', '.sql', '.cls', '.txt', '.c', '.h')

def get_file_line_count(filename):
lines = 0
with open(filename, 'rb') as fd:
data = fd.read()
fd.close()
lines += data.count('\n')
if not data.endswith('\n'):
lines += 1
print("%s : %d" % (filename, lines))
return lines

def get_file(fpath):
lines = 0
for roots, dirs, files in os.walk(fpath):
for file in files:
filename = os.path.join(roots, file)
x, ext = os.path.splitext(file)
if ext in exts:
lines += get_file_line_count(filename)
print("all lines count: %d" % lines)

exts = ('.bas', '.frm', '.sql', '.cls', '.txt', '.c', '.h')

if __name__ == '__main__':
get_file('e:\project')
langlang2671 2012-07-30
  • 打赏
  • 举报
回复
使用enumerate
Gloveing 2012-07-27
  • 打赏
  • 举报
回复
所有 *.frm,*.bas 的文件的行数总和,那就一个遍历嘛。

lines_count = 0
for roots,dirs,files in os.walk('.'):
for file in files:
if file[-4:]=='.frm' or file[-4:]=='.bas'
f = open(file)
data = f.read()
f.close()
lines_count += data.count('\n')

print "all lines count:%d" %lines_count
Gloveing 2012-07-27
  • 打赏
  • 举报
回复
f = open('show_3d.py')
data = f.read()
f.close()
print data.count('\n')
Gloveing 2012-07-27
  • 打赏
  • 举报
回复
把文件读入一个字符串,统计'\n'的个数
Gloveing 2012-07-27
  • 打赏
  • 举报
回复
使用'gbk' 解码。。。
bugs2k 2012-07-27
  • 打赏
  • 举报
回复
文件包含非 ascii 编码
zzy0805 2012-07-27
  • 打赏
  • 举报
回复
这是什么原因

Traceback (most recent call last):
File "D:/ywgl2/a2.py", line 13, in <module>
data = f.read()
UnicodeDecodeError: 'gbk' codec can't decode bytes in position 598-599: illegal multibyte sequence
xiaoxiao8372 2012-07-27
  • 打赏
  • 举报
回复
max(i for i,x in enumerate(open(filepath,mode)) + 1
angel_su 2012-07-27
  • 打赏
  • 举报
回复
file文件名是basename来的,不是整个绝对路径,按你的代码应该是改成:
f = open(os.path.join(roots, file))
aa800629 2012-07-27
  • 打赏
  • 举报
回复
import os
lines_count = 0
for roots,dirs,files in os.walk('d:/ywgl/'):
for file in files:
if file[-4:]=='.ini':
f = open(file)
data = f.read()
f.close()
lines_count += data.count('\n')

print ("all lines count:%d" %lines_count)

以上是py文件,想统计 d:\ywgl目录下的 *.ini 文件的行数,但是报错?

Traceback (most recent call last):
File "C:/a.py", line 6, in <module>
f = open(file)
IOError: [Errno 2] No such file or directory: 'MisVer.ini'

注: d:\ywgl下,是肯定有misver.ini 这个文件的。

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧