remove_symlinks.py 954 B

1234567891011121314151617181920212223242526
  1. import os
  2. import sys
  3. def remove_symlinks(root_dir, output_file="removed_symlinks.txt"):
  4. removed = []
  5. for dirpath, _, filenames in os.walk(root_dir):
  6. for fname in filenames:
  7. fpath = os.path.join(dirpath, fname)
  8. try:
  9. if os.path.islink(fpath):
  10. os.unlink(fpath)
  11. removed.append(fpath)
  12. except Exception as e:
  13. print(f"Error removing symlink {fpath}: {e}")
  14. with open(output_file, "w", encoding="utf-8") as f:
  15. for path in removed:
  16. f.write(path + "\n")
  17. print(f"Removed {len(removed)} symlinks. Results saved to {output_file}")
  18. if __name__ == "__main__":
  19. if len(sys.argv) < 2:
  20. print("Usage: python discard_media.py <directory> [output_file]")
  21. else:
  22. root = sys.argv[1]
  23. out_file = sys.argv[2] if len(sys.argv) > 2 else "removed_symlinks.txt"
  24. remove_symlinks(root, out_file)