examples/python/project/traversing_dependency_tree.py

import sys
import os

if len(sys.argv) < 2:
   exit("Usage: {} NAME".format(sys.argv[0]))

start = sys.argv[1]

def get_dependencies(name):
   print("Processing {}".format(name))

   deps = set(name)
   filename = name + ".txt"
   if not os.path.exists(filename):
       return deps

   with open(filename) as fh:
       for line in fh:
           row = line.rstrip("\n")
           deps.add(row)
           deps.update( get_dependencies(row) )

   return deps

dependencies = get_dependencies(start)
print(dependencies)

examples/python/project/a.txt

b
c
d

examples/python/project/b.txt

e
d

examples/python/project/a.txt

b
c
d

$ python traversing_dependency_tree.py a

Processing a
Processing b
Processing e
Processing d
Processing c
Processing f
Processing g
Processing d
{'d', 'g', 'c', 'b', 'a', 'e', 'f'}