Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- from collections import Counter
- dir_path = os.getcwd()
- files = [f for f in os.listdir(dir_path) if f.endswith('.txt')]
- unique_lines = set()
- for file in files:
- with open(os.path.join(dir_path, file), 'r') as f:
- lines = [line.strip() for line in f.readlines()]
- unique_lines.update(lines)
- line_counts = Counter()
- for file in files:
- with open(os.path.join(dir_path, file), 'r') as f:
- lines = [line.strip() for line in f.readlines()]
- line_counts.update(lines)
- common_lines = [line for line, count in line_counts.items() if count == len(files)]
- for file in files:
- new_filename = os.path.join(os.path.dirname(file), "c_" + os.path.basename(file))
- with open(os.path.join(dir_path, file), 'r') as f:
- with open(new_filename,'w', encoding='utf-8') as new_file:
- lines = [line.strip() for line in f.readlines()]
- new_lines = [line for line in lines if line not in common_lines]
- new_file.write('\n'.join(new_lines))
- print("Success.")
Advertisement
Add Comment
Please, Sign In to add comment