Python Files Tracker for Reducing Time Consuming Tasks

Do you want to know how many python files you create or update each year? Or do you need review actions to be completed next month?

Or you maybe run machine learning python models located in different folders, and find that it takes extra time to get back after switching priorities or working on different projects.

Here is the tool that can help with this – Python Files Tracker. The intent of this tool is eliminate time-consuming task of keeping track of python files with machine learning or other code. The tool allow to automate gathering and formatting information from notes saved in the comments.

How it works.
You put some notes in python comments section created with triple quotation marks.

Then run python script and it will extract special notes plus file name, last modified date and save information in csv file. So you will have in one place all python file names and notes like what do next or what was wrong with the last run of machine learning model or what parameters were used.
The output information is saved in CSV file.

Very simple.

More Details on How to Use

The tool will extract notes that start from :. and end with .: and located within first python comment section. This section should start with triple quotation marks “”” and end also with quotation marks “””. Screenshot below demonstrates inserting specific notes for extraction by Files Tracker Tool:

Few special labels (intent, next action) can be inserted in the notes. The note with ‘intent’ label that is following after opening tag :. will be placed in the column Intent in CSV output file. And the note with ‘next action’ goes to Next action column.

If there is no label the text within :. and .: will be placed in Notes column in the output information.

The input to Python Files Tracker is folder or folders where python files are located. Only top level folder is required to specify. Python Files Tracker will look then in all sub folders. This is specified in the beginning of string in folders_top_level variable.

The tool is looking in all python files that have extension .py which is specified in variable ext. You can change extension and use the same for different files (for example php files). As long as the files are text files it will work.

Below is the source code for Python Files Tracker. Feel free to provide comments, feedback, request for adding different features. I would love to hear what do you think about the tool or how it works for you.

# -*- coding: utf-8 -*-
"""
Python Files Tracker
        
For updates, comments, requests visit
 https://intelligentonlinetools.com/blog/2019/07/10/python-files-tracker/        
        
Do not remove the this header
"""
import os
import csv
from datetime import datetime

ext=".py"
#INSERT here your own folders - you can have any number of folders separated by ; or just one folder. 
folders_top_level="C:\\Users\\Desktop\\folder1;C:\\Users\\folder2"


def find_between( s, first, last ):
  
   
    start_length=len(first)
    try:
        start = s.index( first ) + len( first )
        end = s.index( last, start )
        return s[start+start_length:end]
    except ValueError:
        return ""

def get_notes_from_file(filename):
    tag_dictionary = {}
    file_txt = open(filename, 'r', encoding="utf8")
    source_code = file_txt.read() 
    comments=find_between(source_code, '"""', '"""')
   
    print (comments)
    start_pos=0
    done=False
    tag_dictionary['intent'] =""
    tag_dictionary['next action'] =""
    tag_dictionary['notes'] =""
  
    while not done:
        
        open_tag=comments.find(":.",  start_pos)
        if (open_tag >= 0) :
           close_tag = comments.index(".:", open_tag+2)
           if comments[open_tag+2:open_tag+16].find('intent') >=0 :
                      tag_dictionary['intent'] = comments[open_tag+2:close_tag-2]
           elif comments[open_tag+2:open_tag+16].find('next action') >=0 :
                      tag_dictionary['next action'] = comments[open_tag+2:close_tag-2]
           else :
                      tag_dictionary['notes']  = comments[open_tag+2:close_tag-2]

           start_pos = open_tag+2
        else:
            done=True
    return tag_dictionary       
        


def create_dir(dirName):
    if not os.path.exists(dirName):
        os.mkdir(dirName)
        print("Directory " , dirName ,  " Created ")
    else:    
        print("Directory " , dirName ,  " already exists")
        
def get_file_contents(fname):
   
    source_file = open(fname, 'r')
    source_code = source_file.read() 
    return source_code         
 
def pywalker(path):
    
   with open('data_files_tracker.csv', 'a', encoding="utf8", newline='' ) as csvfile: 
    fieldnames = ['File', 'Intent', 'Next action', 'Last modified','Notes']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    writer.writeheader()  
   
    print ("start")
    for root, dirs, files in os.walk(path):
        
        for dir_ in dirs:
            print ( os.path.join(root, dir_) )
            
        for file_ in files:
           
            if file_.lower()[-len(ext):] == ext:
                  print( os.path.join(root, file_) )
                  full_fname=os.path.join(root, file_)
                  extracted_info=get_notes_from_file(full_fname)

                  print (extracted_info['intent'])

                  print (datetime.fromtimestamp(os.path.getmtime(os.path.join(root, file_))))
                  
                  last_modified=datetime.fromtimestamp(os.path.getmtime(full_fname))

                  writer.writerow({'File': os.path.join(root, file_), 'Intent': extracted_info['intent'], 'Next action': extracted_info['next action'], 'Last modified':last_modified, 'Notes': extracted_info['notes'] })
 
if __name__ == '__main__':
  
    folders= folders_top_level.split(";")
    for folder in folders:
        pywalker(folder)     

References
1. Python 101 how to traverse a directory/



Leave a Comment