每日代碼統計
阿新 • • 發佈:2017-09-04
sca exception expect rom color author log 監聽 use
最近工作不是很忙,在學習《python核心編程3》,給自己定了一個小目標,每天編碼1000行,由於我一邊看書,一邊敲例子,都是在demo目錄下面進行的,
所以寫了個監聽demo目錄的腳本,用來統計每天的編碼行數,實現思路就是,遍歷demo下的所有文件,找到今日創建的文件,將編碼行數累加,下面是代碼,
歡迎指正!
# -*- coding: utf-8 -*- """ author:ruiql date:2017/9/4 version:1.0 指定一個目錄,針對當日的編碼情況,做行數統計 """ import os, sys import time from datetime import datetime EXPECT_ROWS= 1000 #預期完成行數 SCAN_DIR = ‘/Users/tuyoo/pythondemo/demo‘ #掃描路徑 def statisics_rows(file_list): row_count = 0 for file in file_list: print ‘#‘,file.name row_count += len(file.readlines()) file.close() print ‘今日完成編碼行數:%s, 目標行數:%s, 完成度:%0.2f%%‘ % (row_count, EXPECT_ROWS, row_count*1.0/EXPECT_ROWS*100)def scan_dir_list_today_create(scan_dir): ‘‘‘ 掃描目錄,得到今日創建的文件列表 :param scan_dir: :return: ‘‘‘ result = [] if not os.path.isdir(scan_dir): raise Exception _recur_dir_get_file(scan_dir, result) return result def _recur_dir_get_file(sub_dir, result): forparent, dirnames, filenames in os.walk(sub_dir): for file in filenames: print parent+‘/‘+file file = _is_today_create(parent+‘/‘+file) if file: result.append(file) def _is_today_create(file): create_time = os.path.getctime(file) f_create_time = datetime.fromtimestamp(create_time) now = datetime.now() if (now - f_create_time).days == 0: return open(file) return False statisics_rows(scan_dir_list_today_create(SCAN_DIR))
每日代碼統計