Daily code statistics and code statistics
I am not very busy recently. I learned "python core programming 3" and set a small goal for myself. I code 1000 lines a day. As I read a book and give an example, all are performed under the demo directory,
Therefore, I wrote a script to listen to the demo directory to count the number of encoding lines per day. The idea is to traverse all the files under the demo, find the files created today, and accumulate the number of encoding lines, below is the code,
Thank you!
#-*-Coding: UTF-8-*-"author: ruiqldate: version: 1.0 specifies a directory and counts the number of lines for the encoding of the day." "import OS, sysimport timefrom datetime import datetimeEXPECT_ROWS = 1000 # expected number of completed rows SCAN_DIR = '/Users/tuyoo/pythondemo/demo' # scan path def statisics_rows (file_list): row_count = 0 for file in file_list: print '#', file. name row_count + = len (file. readlines () file. close () print 'number of completed encoding lines today: % s, target number of lines: % s, completion: % 0.2f % '% (row_count, EXPECT_ROWS, row_count * 1.0/EXPECT_ROWS * 100) def scan_dir_list_today_create (scan_dir): ''' scan directory to obtain the list of files created today: 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 = _ 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 ))