The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Create a file search utility with tags

Here is a complete implementation of a file search utility with tagging functionality using Python. This utility allows users to:

  • Tag files with custom labels

  • Search for files by name or tags

  • Maintain a simple local database (JSON-based)

python
import os import json from pathlib import Path # Path to the tag database DB_FILE = "file_tags.json" def load_tags(): if os.path.exists(DB_FILE): with open(DB_FILE, "r") as f: return json.load(f) return {} def save_tags(tags): with open(DB_FILE, "w") as f: json.dump(tags, f, indent=4) def add_tag(file_path, tag): file_path = str(Path(file_path).resolve()) tags = load_tags() if file_path not in tags: tags[file_path] = [] if tag not in tags[file_path]: tags[file_path].append(tag) save_tags(tags) print(f"Tag '{tag}' added to '{file_path}'") else: print(f"File already tagged with '{tag}'") def remove_tag(file_path, tag): file_path = str(Path(file_path).resolve()) tags = load_tags() if file_path in tags and tag in tags[file_path]: tags[file_path].remove(tag) if not tags[file_path]: del tags[file_path] save_tags(tags) print(f"Tag '{tag}' removed from '{file_path}'") else: print(f"Tag not found for this file") def search_files_by_tag(tag): tags = load_tags() results = [file for file, tag_list in tags.items() if tag in tag_list] if results: print(f"Files tagged with '{tag}':") for r in results: print(r) else: print(f"No files found with tag '{tag}'") def search_files_by_name(name_query, search_dir="."): print(f"Searching for files containing '{name_query}' in '{search_dir}'...") for root, dirs, files in os.walk(search_dir): for file in files: if name_query.lower() in file.lower(): print(os.path.join(root, file)) def list_all_tags(): tags = load_tags() all_tags = set(tag for tag_list in tags.values() for tag in tag_list) print("All tags in the database:") for t in sorted(all_tags): print(t) def main(): print("File Tag & Search Utility") print("=========================") print("Commands:") print(" add_tag <file_path> <tag>") print(" remove_tag <file_path> <tag>") print(" search_tag <tag>") print(" search_name <name_query> [search_dir]") print(" list_tags") print(" exit") while True: cmd = input(">> ").strip().split() if not cmd: continue command = cmd[0] if command == "add_tag" and len(cmd) == 3: add_tag(cmd[1], cmd[2]) elif command == "remove_tag" and len(cmd) == 3: remove_tag(cmd[1], cmd[2]) elif command == "search_tag" and len(cmd) == 2: search_files_by_tag(cmd[1]) elif command == "search_name": if len(cmd) == 2: search_files_by_name(cmd[1]) elif len(cmd) == 3: search_files_by_name(cmd[1], cmd[2]) else: print("Usage: search_name <name_query> [search_dir]") elif command == "list_tags": list_all_tags() elif command == "exit": break else: print("Unknown command or incorrect usage.") if __name__ == "__main__": main()

Features:

  • Tags are stored in a file_tags.json file in the current directory.

  • Search files either by tag or by name within a directory.

  • Interactive command-line interface.

You can enhance this further by integrating a GUI, handling file deletion safely, or syncing tags across systems with a shared database. Let me know if you want a web or desktop GUI version too.

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About