Daily code statistics and code statistics

Source: Internet
Author: User

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 ))

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.