genericPaster

Uniquify

Jun 7th, 2024
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.02 KB | None | 0 0
  1. import os
  2. from collections import Counter
  3.  
  4. dir_path = os.getcwd()
  5.  
  6. files = [f for f in os.listdir(dir_path) if f.endswith('.txt')]
  7. unique_lines = set()
  8. for file in files:
  9.     with open(os.path.join(dir_path, file), 'r') as f:
  10.         lines = [line.strip() for line in f.readlines()]
  11.     unique_lines.update(lines)
  12. line_counts = Counter()
  13. for file in files:
  14.     with open(os.path.join(dir_path, file), 'r') as f:
  15.         lines = [line.strip() for line in f.readlines()]
  16.         line_counts.update(lines)
  17. common_lines = [line for line, count in line_counts.items() if count == len(files)]
  18. for file in files:
  19.     new_filename = os.path.join(os.path.dirname(file), "c_" + os.path.basename(file))
  20.     with open(os.path.join(dir_path, file), 'r') as f:
  21.         with open(new_filename,'w', encoding='utf-8') as new_file:
  22.             lines = [line.strip() for line in f.readlines()]
  23.             new_lines = [line for line in lines if line not in common_lines]
  24.             new_file.write('\n'.join(new_lines))
  25. print("Success.")
  26.  
  27.  
  28.  
Advertisement
Add Comment
Please, Sign In to add comment