已有文件:
'COUNT.txt'用来保存统计数据
'countLine.py'代码如下:
1 # coding=utf-8 2 import os 3 import time 4 basedir = '/Users/Hoa/Desktop/ET/DPplatform/' 5 filelists = [] 6 totalline = 0 7 ISOTIMEFORMAT='%Y-%m-%d %X' 8 # 指定想要统计的文件类型 9 whitelist = ['m', 'swift', 'h'] 10 #遍历文件, 递归遍历文件夹中的所有 11 def getFile(basedir): 12 global filelists 13 for parent,dirnames,filenames in os.walk(basedir): 14 #for dirname in dirnames: 15 # getFile(os.path.join(parent,dirname)) #递归 16 for filename in filenames: 17 ext = filename.split('.')[-1] 18 #只统计指定的文件类型,略过一些log和cache文件 19 if ext in whitelist: 20 filelists.append(os.path.join(parent,filename)) 21 #统计一个文件的行数 22 def countLine(fname): 23 count = 0 24 for file_line in open(fname).xreadlines(): 25 if file_line != '' and file_line != ' ': #过滤掉空行 26 count += 1 27 #print fname + '----' , count 28 return count 29 if __name__ == '__main__' : 30 startTime = time.clock() 31 getFile(basedir) 32 for filelist in filelists: 33 totalline = totalline + countLine(filelist) 34 print '代码总共有',totalline,'行' 35 print 'Done! Cost Time: %0.2f second' % (time.clock() - startTime) 36 with open('COUNT.txt', 'a+') as f: 37 f.write('总代码行数:%s行 检查日期:%s '%(totalline,time.strftime( ISOTIMEFORMAT, time.localtime())))
'thirdLibLine.py'代码如下:
1 # coding=utf-8 2 import os 3 import time 4 basedir = '/Users/Hoa/Desktop/ET/DPplatform/DPplatform/ThirdLibrary' 5 filelists = [] 6 totalline = 0 7 ISOTIMEFORMAT='%Y-%m-%d %X' 8 # 指定想要统计的文件类型 9 whitelist = ['m', 'swift', 'h'] 10 #遍历文件, 递归遍历文件夹中的所有 11 def getFile(basedir): 12 global filelists 13 for parent,dirnames,filenames in os.walk(basedir): 14 #for dirname in dirnames: 15 # getFile(os.path.join(parent,dirname)) #递归 16 for filename in filenames: 17 ext = filename.split('.')[-1] 18 #只统计指定的文件类型,略过一些log和cache文件 19 if ext in whitelist: 20 filelists.append(os.path.join(parent,filename)) 21 #统计一个文件的行数 22 def countLine(fname): 23 count = 0 24 for file_line in open(fname).xreadlines(): 25 if file_line != '' and file_line != ' ': #过滤掉空行 26 count += 1 27 # print fname + '----' , count 28 return count 29 if __name__ == '__main__' : 30 startTime = time.clock() 31 getFile(basedir) 32 for filelist in filelists: 33 totalline = totalline + countLine(filelist) 34 print '第三方占',totalline,'行' 35 print 'Done! Cost Time: %0.2f second' % (time.clock() - startTime) 36 with open('COUNT.txt', 'a+') as f: 37 f.write('第三方代码行数:%s行 检查日期:%s '%(totalline,time.strftime( ISOTIMEFORMAT, time.localtime())))
'start.command'代码如下:
#!/usr/bin/env python # coding=utf-8 import os os.system('python /Users/Hoa/Documents/代码行数统计/countLine.py') os.system('python /Users/Hoa/Documents/代码行数统计/thirdLibLine.py')
问题描述:
执行start.command,无法将信息写入COUNT.txt。
直接在终端执行py文件是可以写入的。
附运行command的图如下 :