Advertisement
genericPaster

remove_common

May 8th, 2024
628
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.15 KB | None | 0 0
  1. import os
  2. from collections import Counter
  3.  
  4. # Will grab all txt files in directory you are in when running the script
  5. # Then it will go over them and count the lines in all the files that are the same
  6. # Lines that are common across all files will be deleted
  7.  
  8. dir_path = os.getcwd()
  9.  
  10. files = [f for f in os.listdir(dir_path) if f.endswith('.txt')]
  11. unique_lines = set()
  12.  
  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.        
  17.     unique_lines.update(lines)
  18.  
  19. line_counts = Counter()
  20. for file in files:
  21.     with open(os.path.join(dir_path, file), 'r') as f:
  22.         lines = [line.strip() for line in f.readlines()]
  23.         line_counts.update(lines)
  24.  
  25. common_lines = [line for line, count in line_counts.items() if count == len(files)]
  26.  
  27. for file in files:
  28.     with open(os.path.join(dir_path, file), 'r+') as f:
  29.         lines = [line.strip() for line in f.readlines()]
  30.         new_lines = [line for line in lines if line not in common_lines]
  31.         f.seek(0)
  32.         f.write('\n'.join(new_lines))
  33.         f.truncate()
  34.  
  35. print("Docs cleaned of common info.")
  36.  
  37.  
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement