I am building a file tagging system using Python. The idea is simple. Given a directory of files (and files within subdirectories), I want to filter them out using a filter input and tag those files with a word or a phrase.
If I got the following contents in my current directory:
data/
budget.xls
world_building_budget.txt
a.txt
b.exe
hello_world.dat
world_builder.spec
and I execute the following command in the shell:
py -3 tag_tool.py -filter=world -tag="World-Building Tool"
My output will be:
These files were tagged with "World-Building Tool":
data/ world_building_budget.txt
hello_world.dat
world_builder.spec
My current output isn't exactly like this but basically, I am converting all files and files within subdirectories into a single dictionary like this:
def fs_tree_to_dict(path_):
file_token = ''
for root, dirs, files in os.walk(path_):
tree = {d: fs_tree_to_dict(os.path.join(root, d)) for d in dirs}
tree.update({f: file_token for f in files})
return tree
Right now, my dictionary looks like this: key:''
.
In the following function, I am turning the empty values ''
into empty lists (to hold my tags):
def empty_str_to_list(d):
for k,v in d.items():
if v == '':
d[k] = []
elif isinstance(v, dict):
empty_str_to_list(v)
When I run my entire code, this is my output:
hello_world.dat ['World-Building Tool']
world_builder.spec ['World-Building Tool']
But it does not see data/world_building_budget.txt
. This is the full dictionary:
{'data': {'world_building_budget.txt': []},
'a.txt': [],
'hello_world.dat': [],
'b.exe': [],
'world_builder.spec': []}
This is my full code:
import os, argparse
def fs_tree_to_dict(path_):
file_token = ''
for root, dirs, files in os.walk(path_):
tree = {d: fs_tree_to_dict(os.path.join(root, d)) for d in dirs}
tree.update({f: file_token for f in files})
return tree
def empty_str_to_list(d):
for k, v in d.items():
if v == '':
d[k] = []
elif isinstance(v, dict):
empty_str_to_list(v)
parser = argparse.ArgumentParser(description="Just an example", formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--filter", action="store", help="keyword to filter files")
parser.add_argument("--tag", action="store", help="a tag phrase to attach to a file")
parser.add_argument("--get_tagged", action="store", help="retrieve files matching an existing tag")
args = parser.parse_args()
filter = args.filter
tag = args.tag
get_tagged = args.get_tagged
current_dir = os.getcwd()
files_dict = fs_tree_to_dict(current_dir)
empty_str_to_list(files_dict)
for k, v in files_dict.items():
if filter in k:
if v == []:
v.append(tag)
print(k, v)
elif isinstance(v, dict):
empty_str_to_list(v)
if get_tagged in v:
print(k, v)