Python script to add Obsidian tag to files.

import argparse
import os
import sys
def getFiles(_path,_ext,_debug):
  rtn_list = []
  if (_debug > 0):
    print(f"  getFiles({_path},{_ext},{_debug})")
    
  list_dir = os.listdir(_path)
  if not list_dir:
    print("  **getFiles returned nothing **")
  else:
    for x in list_dir:
      if (_debug > 0):
        print(f"  Found: {x}")
      
      if x.endswith(_ext):
        rtn_list.append(x)
        if (_debug > 0):
          print(f"  --Match EXT:{_ext} == {x}")
      else:
        if (_debug > 0):
          print(f"  --Skip EXT:{_ext} != {x}")
  
  return rtn_list

def readFile(_file,_debug):
  if (_debug > 0):
    print(f"  readFile({_file},{_debug})")
  file = open(_file)
  lines = file.readlines()
  print(lines)
  file.close()

def checkFile(_file,_tag,_debug):
  chk_tag = "UNKNOWN"
  if (_debug > 0):
    print(f"  checkFile({_file},{_tag},{_debug})")
    
  chkFile = open(_file,'r')
  cnt = 0
  while True:
    cnt += 1
    line = chkFile.readline()
    if not line:
      break
    print("  Line{}: {}".format(cnt, line.strip()))
    
    if (_tag in line.split()):
      print(f"  Tag:{_tag} Already exists in file:{x}")
      chk_tag = "FOUND_TAG"
    
  chkFile.close()
  
  return chk_tag

def tagFile(_file,_tag,_debug):
  if (_debug > 0):
    print(f"  checkFile({_file},{_tag},{_debug})")
    
  if not _tag:
    print(f"  tagFile: Error - cannot continue without tag!")
    return "ERROR: Missing Tag!"
    
  with open(_file, 'r') as original: data = original.read()
  with open(_file, 'w') as modified: modified.write(_tag + "\n" + data)
  return "Tagged"
  

def fromARRAY(_list,_debug):
  print("  ** FROM ARRAY **")
  r_list = []
  i = 0
  for y in _list:
    i += 1
    print(f"  {i}: {y}")
  
  ans = input("Enter Selection (x~exit) :")
  if not ans:
    ans="*"
    
  if (ans.lower() == "x"):
    print("  Good-Bye")
    quit()
  
  i = 0
  print(f"reset i:{i}")
  for y in _list:
    i += 1
    if (ans == "*"):
      r_list.append(y)
      if (_debug > 0):
        print(f"Append:{i} - {y}")
    else:
      if (ans == str(i)):
        r_list.append(y)
        if (_debug > 0):
          print(f"Append:{i} - {y}")
  return r_list

#####################################
## END DEFS
#####################################

parser = argparse.ArgumentParser()
parser.add_argument("-p", "--path")
parser.add_argument("-f", "--file")
parser.add_argument("-e", "--ext", default=".md")
parser.add_argument("-t", "--tag", default="#docker")
parser.add_argument("-d", "--debug", default=0)

args = parser.parse_args()
if args.path is not None:
  _path = args.path
else:
  _path = os.getcwd()
  
_debug = int(args.debug)
_ext = args.ext
_file = args.file
_tag = args.tag

if (_debug > 0):
  print(f"  {args}")
  
if not os.path.isdir(_path):
  print(f"  Invalid Path:{_path}")
  quit()

#########################################
## END ARGS
#########################################
f_list = []
if (_file):
  if not os.path.isfile(_file):
    print(f"  Invaild File:{_file}")
  else:
    f_list.append(_file)
else:
  f_list = getFiles(".",_ext,_debug)

if (len(f_list) == 1):
  print(f"  Single: f_list:{f_list}")
else:
  f_list = fromARRAY(f_list,_debug)

if not f_list:
  print("Nothing to process")
  quit()
  
for x in f_list:
  print(f"  Processing: {x}")
  data = checkFile(x,_tag,_debug)
  print(f"  DATA:{data}")
  if (data == "UNKNOWN"):
    tagIt = tagFile(x,_tag,_debug)
    print(f"  TAG_IT:{tagIt}")