每日代碼統計,代碼統計

來源:互聯網
上載者:User

每日代碼統計,代碼統計

最近工作不是很忙,在學習《python核心編程3》,給自己定了一個小目標,每天編碼1000行,由於我一邊看書,一邊敲例子,都是在demo目錄下面進行的,

所以寫了個監聽demo目錄的指令碼,用來統計每天的編碼行數,實現思路就是,遍曆demo下的所有檔案,找到今日建立的檔案,將編碼行數累加,下面是代碼,

歡迎指正!

# -*- coding: utf-8 -*-"""author:ruiqldate:2017/9/4version:1.0指定一個目錄,針對當日的編碼情況,做行數統計"""import os, sysimport timefrom datetime import datetimeEXPECT_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 resultdef _recur_dir_get_file(sub_dir, result):    for parent, 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 Falsestatisics_rows(scan_dir_list_today_create(SCAN_DIR))

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.