# unity_parts_compare.py
# Usage:
# python unity_parts_compare.py
from pathlib import Path
# ===== CONFIGURATION =====
# Set your folder path here (works with Windows, Mac, Linux)
# Examples:
# r"C:\Users\Vinay\Desktop\unity"
# "C:/Users/Vinay/Desktop/unity"
# "/Users/vinay/Desktop/unity"
INPUT_FOLDER = Path(r"C:\Users\Vinay\Desktop\unity")
OUTPUT_FOLDER = INPUT_FOLDER # Put all results back in the same folder
OUTPUT_COMMON_NAME = "unity_common_parts.txt" # Parts common to ALL files
OUTPUT_UNIQUE_SUFFIX = "_unique.txt" # Deduped list per file
OUTPUT_EXCLUSIVE_SUFFIX = "_exclusive.txt" # Only in this file, nowhere else
OUTPUT_NONCOMMON_SUFFIX = "_noncommon.txt" # In this file, but not common-to-all
OUTPUT_SUMMARY_NAME = "unity_summary.txt" # Summary report
# =========================
def read_set(path: Path) -> set:
s = set()
with path.open("r", encoding="utf-8", errors="ignore") as f:
for line in f:
part = line.rstrip("\n\r")
if part:
s.add(part)
return s
def safe_name(p: Path) -> str:
return p.stem.replace(" ", "_")
def main():
if not INPUT_FOLDER.exists() or not INPUT_FOLDER.is_dir():
print(f"ERROR: Input folder '{INPUT_FOLDER}' not found.")
return
OUTPUT_FOLDER.mkdir(parents=True, exist_ok=True)
files = sorted(INPUT_FOLDER.glob("*.txt"))
if len(files) < 2:
print(f"Need at least two .txt files inside '{INPUT_FOLDER}'. Found {len(files)}.")
return
sets = [(p, read_set(p)) for p in files]
# Common to ALL
common_all = set.intersection(*(s for _, s in sets))
common_path = OUTPUT_FOLDER / OUTPUT_COMMON_NAME
with common_path.open("w", encoding="utf-8") as out:
for part in sorted(common_all):
out.write(part + "\n")
union_all = set.union(*(s for _, s in sets))
summary_lines = [f"Processed folder: {INPUT_FOLDER.resolve()}",
f"Files: {len(sets)}", ""]
for p, s in sets:
base = safe_name(p)
# Unique (deduped per file)
uniq_path = OUTPUT_FOLDER / f"{base}{OUTPUT_UNIQUE_SUFFIX}"
with uniq_path.open("w", encoding="utf-8") as out:
for part in sorted(s):
out.write(part + "\n")
# Exclusive (only in this file)
exclusive = s - (union_all - s)
excl_path = OUTPUT_FOLDER / f"{base}{OUTPUT_EXCLUSIVE_SUFFIX}"
with excl_path.open("w", encoding="utf-8") as out:
for part in sorted(exclusive):
out.write(part + "\n")
# Non-common (remove global common)
noncommon = s - common_all
noncommon_path = OUTPUT_FOLDER / f"{base}{OUTPUT_NONCOMMON_SUFFIX}"
with noncommon_path.open("w", encoding="utf-8") as out:
for part in sorted(noncommon):
out.write(part + "\n")
summary_lines.append(
f"{p.name}: {len(s)} unique | {len(exclusive)} exclusive | {len(noncommon)} non-common"
)
summary_lines.append("")
summary_lines.append(
f"Common to ALL {len(sets)} files: {len(common_all)} -> {OUTPUT_COMMON_NAME}"
)
# Save summary
summary_path = OUTPUT_FOLDER / OUTPUT_SUMMARY_NAME
with summary_path.open("w", encoding="utf-8") as f:
f.write("\n".join(summary_lines))
print("\n".join(summary_lines))
if __name__ == "__main__":
main()